Solved

Not permanently setting variable?

Posted on 2004-08-24
18
288 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

615 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