Two C++ questions

Discussion in 'OT Technology' started by Saicho, Nov 14, 2006.

  1. Saicho

    Saicho New Member

    Joined:
    Feb 13, 2006
    Messages:
    359
    Likes Received:
    0
    Location:
    Seattle
    Heya all! I'm a Java programmer trying to learn C++ and I have two pretty simple questions.

    1. When compiling:

    Code:
    class A {
      public:
        static int i = 5;
    };
    
    int main() {
      return A.i;
    } 
    I get a compiler error on line 8 "error: expected primary-expression before '.' token. This is the first time I've seen anyhting like this before and I have no idea what it means. What I'm trying to do above is just access a static variable inside the A class. Can anyone explain what's going on here?

    2. When compiling

    Code:
    class A {
      public:
        int i;
        A(int x) { i = x; }
    };
    
    class B {
      public:
        A a;
        B() { a = 5; } 
    };
    
    int main() { 
      B b;
      return b.a.i;
    } 
    
    I also get an error, something about no matching function for call to A::A(). It's pretty obvious what I'm trying to do here. I think the problem is that A a; is calling the default constructor for A but I can't seem to be able to fix the B class to include a as a class variable and have a initialized as an A class with i=5...

    Any advice/explanation is appreciated. Thanks very much for your time!
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    you're using return all wrong dude. I am not sure what you're trying to use main for but return is used in functions and methods to pass a value back to the function or method that called it. You would have to do something like this if you wanted to use function/methods to access i(from first example):

    Code:
    class A {
      public:
        static int i = 5;
        int iReturn();
    };
    
    int A::iReturn()
    {
        return i;
    }
    
    int main() {
      int returnValue;
      A myObject;
      
      
      returnValue = myObject.iReturn();
    }
    
    since i is public you can access it using myObject.i as well:

    int tempValue = myObject.i;

    Hopefully this clears it up, this isnt much different from Java.
     
    Last edited: Nov 14, 2006
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    your second example is kind of a mess. I can see where you're going with it but review what I said above and hopefully it will make a little more sense.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    1. A.i is is trying to access the member i through an actual instance of A, which you don't have. As GOGZILLA mentioned, the correct way to access a static without an instance is using the scope operator (A::i).

    2. You need to initialize member a using an initialization list on B's constructor

    Code:
         B() : a(5) {};
    
    The initialization list allows you to pass parameters to member object constructors that need them.

    GOGZILLA, main can and should return a value since it's defined to return an int. The return value of main can be read by the system, and is typically used for return codes for programs meant to be used by such things as shell scripts.
     
  5. bigb14

    bigb14 New Member

    Joined:
    Nov 8, 2006
    Messages:
    519
    Likes Received:
    0
    just a rookie... but as i understand it return statements have to be inside the function of the thing you want to return... not in you main function. Also, for a easier way to return, just use say

    int FunctionName (int&);
    then it automatically returns the changed variable
     
  6. Saicho

    Saicho New Member

    Joined:
    Feb 13, 2006
    Messages:
    359
    Likes Received:
    0
    Location:
    Seattle
    Thanks guys, I know what main should return, that was just code that I was playing around with to see what would compile and what would not.

    I understand what was wrong before, what I was looking for was:

    1. Since there is no instance of A in main I need to use the :: operator instead of .
    Also, I need to make int i static AND const or I can't access it without making an instanfce of A in main.

    2. skinjob did a very nice explanation of initialization lists, thanks!
     
  7. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    yeah i know that about main, i couldnt figure out exactly what he was thinking main did.
     
  8. bigb14

    bigb14 New Member

    Joined:
    Nov 8, 2006
    Messages:
    519
    Likes Received:
    0
    exactly what I meant
     
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    hmm...
    main only has 1 line. I took it at face value. I didn't think there was some other implied intent or purpose. It appeared to me that the program has no purpose other than to serve as an exercise to demonstrate access to a static member variable.
     

Share This Page