Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Not permanently setting variable?

Posted on 2004-08-24
18
Medium Priority
?
289 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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…
Suggested Courses

661 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