Solved

Not permanently setting variable?

Posted on 2004-08-24
18
284 Views
Last Modified: 2010-04-01
Hi all.

class CClass{
    private:
        string myString;
    public:
        string getMyString();
        void setMyString(string a);
}


//in a seperate file:
vector<CClass> lines;

void myMethod(vector<CClass> &cont){
    for(int i=0;i<20;i++){
        if(something){
            CClass &c=cont[i];
            c.setMyString("SET");
        }
    }
}


It fails to set the CClass c variable myString permanently from the line:

            c.setMyString("SET");

when I access this variable outside of this method in a seperate method, the variable is not set as I set it in the method myMethod.
0
Comment
Question by:Unimatrix_001
  • 8
  • 6
  • 2
18 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11887800
could you post your setMyString(string a) function?

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11887873
That works fine...I use it in other methods and it's fine. But I can't see why it's not working here.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11887912
Then you can post the code portion where you invoke myMethod()
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11887927
int main(){
    //setup sets up lines filling it etc...
    setup();
    myMethod(lines);
    //method outputs contents of lines
    showCClass(lines);
    return 0;
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11887949
I don't see where lines is declared, maybe a global variable?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11887950
Yes, see original question just after class decleration.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11887993
I've managed to sort it. It was saving it properly. I just wasn't accessing it properly! :)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11888157
Well, you are killing my brain, I have compiled and it works. Are you sure the if(something) is not blocking the code inside?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
ID: 11888216
PS. I am assuming the missing colon at the end of .....    class CClass {}    ..... block is only a typo.

Here is my testing code (in Visual C++):

----------------------------------------------------------------
#include <string>
#include <vector>

using namespace std;

class CClass{
    private:
        string myString;
    public:
        string getMyString()
                  { return myString; }
        void setMyString(string a)
                  { myString = a; }
};

---------------------------------------------------
vector<CClass> lines;

void setup()
{
      CClass *aNew;

      for (int i=0; i<30; i++) {
            aNew = new CClass;
            aNew->setMyString("EMPTY");

            lines.push_back(*aNew);
      }
}

void myMethod(vector<CClass> &cont)
{
    for(int i=0;i<20;i++){
        if(i&1){     // Change elements 1,3,5,7,9,11,13,15,17 and 19 only
            CClass &c=cont[i];
            c.setMyString("SET");
        }
    }
}

void showCClass(vector<CClass> &cont){
    CString str;        // Sorry, I prefer CString, doesn't matters

    for(int i=0;i<20;i++) {
        CClass &c=cont[i];
        str += c.getMyString().c_str();
        str += " ";
    }

      ::AfxMessageBox(str);   // Will show EMPTY SET EMPTY SET EMPTY SET .....
}

----------------------------------------------------

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11888439
Any advance?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11891042
>> I've managed to sort it. It was saving it properly. I just wasn't accessing it properly! :)

Your problem is solved now???


Some remarks:

When adding CClass items to  vector<CClass> lines a copy of the items is stored there. However, CClass has no copy constructor - if the class definition from above is complete. Then, a default copy constructor was used that makes a bitwise copy - what is fatal as the string member wasn't constructed properly (points to same buffer but reference counting hasn't be incremented). That could lead to a problem like that you told us but most likely it would lead to a crash when vector items get deleted at end of program as the internal string buffer was deleted twice. So, you should add default constructor, copy constructor, virtual destructor and operator= to have a properly defined class.

When using global variables - in C++ static class members should be preferred before global variables - you have to define them only in one cpp file. All other cpp sources that need to have access to the global variable must have an 'extern' declaration. If 'lines' would be a static member of CClass it's much easier to handle.

Regards, Alex


0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11891789
Why doesn't  the questioner give any feedback if he/she has solved his/her problem?

If the questioner tells what has been wrong, i have no objections.

Regards, Alex
 
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11892124
I think will be fair from the author to tell us first where was the problem (the unique point I can figure out is the 'something' conditional)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11892465
-------------------
"Why doesn't  the questioner give any feedback if he/she has solved his/her problem?"
Because I sleep and have work to do :) ...I've already said that I solved the problem:

"Comment from Unimatrix_001
Date: 08/25/2004 12:46AM BST
 Your Comment  

I've managed to sort it. It was saving it properly. I just wasn't accessing it properly! :) "
-------------------

"I think will be fair from the author to tell us first where was the problem "
It was a problem in the method that read the variable and me screwing up the polymorphism. :)

-------------------
I will award you some points Jamie for the effort, despite me sorting it out before hand, hence the request to lower them...

Thanks,
Uni
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11892546
Well, every time you post a question to EE you have to share your code to others newcomers to exploit the theme discussed. So it will be nice from you to clearly specify where was your problem, source code included. By example, I still can't understand where was your problem and feel that deserve this explanation for my own "experience database".

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 11892913
"So it will be nice from you to clearly specify where was your problem, source code included. By example, I still can't understand where was your problem and feel that deserve this explanation for my own "experience database"."

My question was in error, and was not related to the error that actually existed, nor the solution I found. I shared my code that I thought was causing the problem. It would be inefficient to share code that contained errors once the solution had been found.

Once more:
I was reading from the wrong object due to an error in the polymorphism code I had. I can't be more clear than that. If a newcomer has this problem and reads this comment then I suggest they pick up a C++ book and read the chapter on polymorphism.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question