Solved

Not permanently setting variable?

Posted on 2004-08-24
18
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

734 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