Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Not permanently setting variable?

Posted on 2004-08-24
18
Medium Priority
?
290 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

916 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