Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,287 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 971 people online right now. Registration is fast and FREE... Join Now!




reference and vector troubles, please help!

 
Reply to this topicStart new topic

reference and vector troubles, please help!

crazyjugglerdrummer
7 Jan, 2009 - 12:56 PM
Post #1

YOUR AD HERE
Group Icon

Joined: 7 Jan, 2009
Posts: 611



Thanked: 83 times
Dream Kudos: 375
My Contributions
I am writing a C++ command line utility in Xcode. The object of the program was that each Question object would display its correct answer and three other random Answers randomly selected from a pool of them. Like a flashcard system. This is also my first time trying to learn C++ vectors.

CODE

#include <iostream>
#include <vector>
using namespace std;

class Answer {
public:
    string a;

    
    Answer(string newa)
    {
        a=newa;
        //I wanted to include something in the constructor
        //that would automaticly add the Answer to a vector or array,
        //but couldn't think how. (return its own address before its constructed?)
    }
    
    bool operator==(Answer& A)
    {
        return (a==A.a);
    }
    
    
    void display()
    {
        cout << a;
    }

};

vector<Answer> answers(20);
/*I'm just using vectors because I don't know how and want to learn
It gives me two errors that say: expected identifier before numeric
constant and expected ',' or '...' before numeric constant.
I was going to make this a static data member, but was unsure how
to proceed
*/


    
void add(Answer& A)
{
    (Answer)answers[answers.size()]=A;
    /*it says: insufficient contextual information to determine type,
    does it have to do with parsing to type Answer?
     */
}


CODE

#include <iostream>
#include <vector>
using namespace std;

#include "Answer.cpp"
//also wondering how you include files in c++ like java does.
//my eclipse java compiler will let you use class A as a return type
//in class B, class B as a return type in A also, so they both depend on each other.
// how do you make sure that a file
//is included only once, like a java import? is it with #ifndef and #define?

class Question {
public:
    
    string question;
    Answer& correctAnswer;
    //please help me with references!
    
    Question(string q,Answer& C)
    //it says that Answer::Question::correctAnswer is uninitialized.
    //why is Question inside of Answer?
    {
        question=q;
        correctAnswer=C;
                
    }
    
    void sameAnswer(Answer& a,Answer& b)
    //I wanted a device to make sure that no two prompted choices were the same
    {
        while (a==b)
        {
            b=answers[(rand()%answers.size())+1];
            //again, insufficient contextual information to determine type
        }
    }
    
    void ask()
    {
        Answer& x=&answers[(rand()%answers.size())+1];
        Answer& y=&answers[(rand()%answers.size())+1];
        Answer& z=&answers[(rand()%answers.size())+1];
        //again, not enough contextual info.
        
        cout << question << endl;
        cout << "A: " << correctAnswer.a << endl;
        cout << "B: " << x.a << endl;
        cout << "C: " << y.a << endl;
        cout << "D: " << z.a << endl;


    }


};







User is offlineProfile CardPM
+Quote Post


Bench
RE: Reference And Vector Troubles, Please Help!
7 Jan, 2009 - 02:52 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
It'd be worth checking your book, or an online reference for exactly how to use vectors. There's a brief tutorial on cprogramming.com
http://www.cprogramming.com/tutorial/stl/vector.html

When you declare a vector, you don't need to specify any size - a vector can start out empty, and resize to suit however many objects you add to it. However, you've chosen to pass a size value to the std::vector<Answer>, which would be OK, however the vector is attempting to default-construct 20 Answer objects, and your Answer class has no default constructor.

I can't see any particular reason why you'd want those 20 default constructed Answer objects, so try starting out with an empty vector instead
vector<Answer> answers;

You can insert items to the vector at any time using push_back
cpp
Answer my_answer( "forty-two" );
answers.push_back( my_answer );




As for references within a class, you need to use the constructor's initialiser list
cpp
class Question {
public:
string question;
Answer& correctAnswer;

Question(string q,Answer& C) : correctAnswer ( C )
{
correctAnswer=C;
}


This post has been edited by Bench: 7 Jan, 2009 - 02:55 PM
User is offlineProfile CardPM
+Quote Post

crazyjugglerdrummer
RE: Reference And Vector Troubles, Please Help!
7 Jan, 2009 - 06:01 PM
Post #3

YOUR AD HERE
Group Icon

Joined: 7 Jan, 2009
Posts: 611



Thanked: 83 times
Dream Kudos: 375
My Contributions
Thanks a lot! I understand the Vector thing because I was getting errors that there wasn't a constructor
Answer:Answer().

It worked perfectly, but I'm not sure I understand the initialiser list thing yet.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Reference And Vector Troubles, Please Help!
8 Jan, 2009 - 11:34 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
An initialiser list is for initialising a class' data members. With a trivial constructor like yours, you could omit everything in the body of your constructor
cpp
class Question {
public:
string question;
Answer& correctAnswer;

Question(string q,Answer& C)
: correctAnswer( C ) , question( q ) {}
This is the 'preferred' way to initialise data members in C++, it uses initialisation syntax rather than copy-initialisation - it allows you to perform direct calls to a data member constructor (Or in the case of a reference, its the only way to initialise a reference member)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 03:18PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month