Trouble with a class variable

This is a discussion on Trouble with a class variable within the C++ Programming forums, part of the General Programming Boards category; I have a class called test with a public variable called abc in it, this variable can be altert in ...

  1. #1
    TravisDane
    Guest

    Trouble with a class variable

    I have a class called test with a public variable called abc in it,
    this variable can be altert in the main (using "test test;" as a
    constructor) but when i try to alter it in a funtion of another class
    it goes wrong! (again using the "test test;" constructor)

    You should know that when im altering it i actually calling a
    function who alters it like:

    test::altervar(string newvar)
    {
    abc=newvar;
    }

    Now when i call another function it still recognizes the previous
    value of abc (given in main in this case) to be its value

    Weird! can't figure it out myself, thx for any help you can give me

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Post your code, itt'l make your question loads easier to answer than trying to interpret your vague description.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Aya, thank you very much:p

    Code:
     class test
    {
    public:
    
    void alterabcvar();
    void printabc();
    
    string abc="test1";
    
    }
    
    void test::alterabcvar(string newabc)
    {
    abc=newabc;  <--- everything fine here abc became newabc
    }
    
    void test::printabc()
    {
    cout << abc; <--- here it only prints test2 once????
    }
    
    void main() // <--- HAHA FEAR ME I USE VOID!!!! HAHAHA
    {
    test test;
    test2 test2;
    
    test.alterabc("test2");
    test.printabc();
    
    test2.test;
    test.printabc();
    
    }
    
    class test2
    {
    public:
    void test();
    }
    
    void test2::test()
    {
    test test;
    test.alterabc("test3");
    }

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    > string abc="test1";
    You can't do that, you need to initilize class members from a constructor. If you need help with those I suggest you check the tutorials at this website.

    > test test;
    Can't do that, you need to use a unique name for your class instances (can't be the same name as the class)

    > void test2::test()
    {
    test testvar;
    test.alterabc("test3");

    }

    I don't know what you're trying to achieve here, but again, you can't use test for a variable name. If you're trying to change the value of the test variable in main you'll need to pass it to the function. You also made numerous typos like "test:rintabc()"
    Fix those too.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I dont know if this is generating an error for you but you must include a semicolon after the class.

    should look like this
    Code:
    class test
    {
    public:
    
    void alterabcvar();
    void printabc();
    
    string abc="test1";
    
    }; //add semicolon
    also add it for test 2 class
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Salem
    > void main() // <--- HAHA FEAR ME I USE VOID!!!! HAHAHA
    How nice - taking pride in your ignorance

    Probably not worth trying to teach you anything (there's no guarantee that you'll pay any attention to it) if you continue to do this - after all, if you're willing to break 1 rule, why not break them all?
    Not ment as an attack to you, its just that so many people tell
    me to use int and i still dont see the point of it, maybe you can
    convince me?

    Anyway,all those little mistakes you found in my code is a result
    of typing an example from my head, it wasnt copy pasted,
    but the variables in my other class start to act weird now too,
    maybe im using them wrong.
    Is there a tutorial about using variables of classes?

  7. #7
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    void alterabcvar();
    Code:
    void test::alterabcvar(string newabc)
    {
    abc=newabc;  <--- everything fine here abc became newabc
    }
    Missmatch! Also why not pass newabc as a reference?

    P.S Donīt use void main() !!!!

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by ripper079
    Code:
    void alterabcvar();
    Code:
    void test::alterabcvar(string newabc)
    {
    abc=newabc;  <--- everything fine here abc became newabc
    }
    Missmatch! Also why not pass newabc as a reference?

    P.S Donīt use void main() !!!!
    He, As i just said i typed that code from my head, and thus
    mistakes are bound to happen, i dint have that mistake in the
    code

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    But for that main,so the program should return a valua
    usually a status, if it whent wrong or right, but what if
    you have a exit(1); in a function of yuor program does it
    return a value then too?

    (not that i use such a devil spaghetti code command)

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    80
    Yes. Actually, the argument for exit() is the value that is returned. So, if you use exit(1), then the integer value of 1 is returned to the environment/OS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  2. Replies: 16
    Last Post: 11-10-2007, 03:51 PM
  3. Access protected base class template variable
    By pjotr in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2007, 05:34 AM
  4. Need help with a class variable.
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 10-20-2005, 11:26 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21