Address will always return true? (class vectors + member vars)

This is a discussion on Address will always return true? (class vectors + member vars) within the C++ Programming forums, part of the General Programming Boards category; For some reason, i cannot get the value of a member from my vector. My compiler tells me this : ...

  1. #1
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68

    Address will always return true? (class vectors + member vars)

    For some reason, i cannot get the value of a member from my vector. My compiler tells me this :

    "32 C:\My Documents\LIAUNG PRESENTATION\Doopey.cpp
    [Warning] the address of `int will always be true"

    I tried different combos of asterisks, point tos, and good old spaces, but to no avail. i.e :

    Code:
     return AllQuestions [ Asked - 1 ].Answer ;
     return *AllQuestions [ Asked - 1 ]->Answer ;
     return *AllQuestions [ Asked - 1 ].Answer ;
    The offending functions are GetEndAnswer and GetEndUserAnswer, which just get members from the last element of a vector of Questions. I have no idea why they returns a 1/true/nonzero - they 'should' return
    10 or 20. Is there something i have missed that makes it try to return a pointer or somesuch?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    int Asked = 0;
    
    class Question {
    public :
    	int Answer;
    	int UserAnswer;
    	Question();
    };
    
    Question::Question ()
    {
    	Answer = 10;
    	UserAnswer = 20;
    	Asked ++;
    }
    
    vector <Question> AllQuestions;
    
    int GetEndAnswer()
    { return AllQuestions [ Asked - 1 ].Answer; }
    
    int GetEndUserAnswer()
    { return AllQuestions [ Asked - 1 ].UserAnswer; }
    
    int main()
    {
    	AllQuestions.push_back ( Question );
    	cout << GetEndAnswer << GetEndUserAnswer;
    	system ("PAUSE");
    	return 0;
    }
    Last edited by littleweseth; 11-26-2003 at 05:53 PM.
    Ph33r the sphericalCUBE

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Code:
    AllQuestions.push_back ( Question );
    What exactly are you trying to do with this line?
    Do not make direct eye contact with me.

  3. #3
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    trying to push a new Question object onto the vector. I 'think' it's legal syntax, or do you have to have a name?
    Ph33r the sphericalCUBE

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Try AllQuestions.push_back ( Question());.
    Last edited by Lurker; 11-26-2003 at 06:11 PM.
    Do not make direct eye contact with me.

  5. #5
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    please dont tell me it doesn't work becaues there ARE no questions on the vector...... would that make the things return 1? I did compile it, and it outputs 11. (or truetrue). glort?

    Still doesnt work, even with the parentheses.
    Ph33r the sphericalCUBE

  6. #6
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Then try a Question variable.
    Do not make direct eye contact with me.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I compiled using Question() and got this:
    Attached Images Attached Images  
    Do not make direct eye contact with me.

  8. #8
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    Code:
    	Question Temp;
    	AllQuestions.push_back ( Temp );
    	cout << GetEndAnswer << GetEndUserAnswer;
    still does exact same thing. It is the push, or the actual functions?
    Ph33r the sphericalCUBE

  9. #9
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    If you are going to be using a vector, you should store the index where you use it.
    Do not make direct eye contact with me.

  10. #10
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Its the functions, try this code:
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    int Asked = 0;
    
    class Question {
    public :
    	int Answer;
    	int UserAnswer;
    	Question();
    };
    
    Question::Question ()
    {
    	Answer = 10;
    	UserAnswer = 20;
    	Asked ++;
    }
    
    vector <Question> AllQuestions;
    
    int main()
    {
    	AllQuestions.push_back ( Question());
    	cout << AllQuestions [ Asked - 1 ].Answer << " " << AllQuestions [ Asked - 1 ].UserAnswer << endl;
    	system ("PAUSE");
    	return 0;
    }
    Do not make direct eye contact with me.

  11. #11
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    looks like that works, yeah. Thanks for the quick responses.
    I winder why it doesnt work as a function? Might have to ponder that one.
    Ph33r the sphericalCUBE

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by littleweseth
    looks like that works, yeah. Thanks for the quick responses.
    I winder why it doesnt work as a function? Might have to ponder that one.
    It didn't work as a function because you forgot the parentheses when calling the function, so it just output the function's address:
    Code:
    cout << GetEndAnswer() << GetEndUserAnswer();

  13. #13
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    repeat above: dont prgram when you start seeing sheep flying across your FoV, crying *zzzZZZZ! zzzZZZ!*

    obivous things like that are the reason why you get others to debug - you tend to see your own code as what you think it is, not what it is.
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

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