CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Vectors

  1. #1
    Join Date
    Mar 2010
    Posts
    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

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    30

    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();
    }

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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 +")");
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vectors

    Quote Originally Posted by joe_stoll View Post
    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/

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Vectors

    Quote Originally Posted by cilu View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured