-
March 2nd, 2010, 05:38 AM
#1
Vectors
Hi, I am quite new to c++ and finding it hard to get used to the added functions - I used to use Java..
Anyway I have to create a class house which has a number of member variables that represent a vehicle which I assume is like this
class house
{
string type;
string colour;
string number;
}
And then make a suitable constructor and method toString to return output
string house::toString()
{
string("House is ( " + type + ", " +
colour + ", " +
number +")");
}
but when I do this it says string house::toString() 'expected primary-expression'... I'm so confused!
After I've done this I need to make house into a vector so I can have additional houses - apparently I need to make a class like this
class HouseGroup
{
private:
vector<string> House;
public:
Void addHouse(House v);
string toString();
}
string toString is meant to create a string representation of the entire collection.. ?
If anyone could explain to me how a vector works and how I would go about it I'd be very grateful! Thanks
-
March 2nd, 2010, 07:29 AM
#2
Re: Vectors
1) You have declared a vector, but you do not do anyting with it.
2) is toString() supposed top be a member of house or HouseGroup ?
(you declared it in Housegroup, but implement it in house).
3) toString() should return a string.
-
March 2nd, 2010, 07:49 AM
#3
Re: Vectors
house::toString() says that the toString function is a member of house and which in your case its not I changed a couple of things.what in your case does vector mean, the only vector i know is a line with an origin,direction and length?.You might want to check out CString classes. I would strongly advise a book on C++ in 24 hours, the fastest way to learn just the basics.
Code:
class house
{
string type;
string colour;
string number;
string toString ();<------------added as a member of house
};<------------added
string toString()<-------------changed
{
string("House is ( " + type + ", " +
colour + ", " +
number +")");
};<------------added
class HouseGroup
{
private:
vector<string> House;
public:
Void addHouse(House v);
string toString();
}
-
March 2nd, 2010, 04:25 PM
#4
Re: Vectors
Code:
string toString()<-------------changed
{
string("House is ( " + type + ", " +
colour + ", " +
number +")");
};<------------added
It used to be better before, except that you had to return something from the function.
Code:
string house::toString()
{
return string("House is ( " + type + ", " + colour + ", " + number +")");
}
-
March 2nd, 2010, 05:12 PM
#5
Re: Vectors
Originally Posted by joe_stoll
what in your case does vector mean, the only vector i know is a line with an origin,direction and length?.You might want to check out CString classes. I would strongly advise a book on C++ in 24 hours, the fastest way to learn just the basics.
Speaking of basics:
http://www.cplusplus.com/reference/stl/vector/vector/
-
March 2nd, 2010, 05:14 PM
#6
Re: Vectors
Originally Posted by cilu
Code:
string toString()<-------------changed
{
string("House is ( " + type + ", " +
colour + ", " +
number +")");
};<------------added
It used to be better before, except that you had to return something from the function.
Code:
string house::toString()
{
return string("House is ( " + type + ", " + colour + ", " + number +")");
}
Actually, that will probably cause a compile error. To avoid it, move the "string(" close paren to just after "House is ( " rather than the end of the line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|