Trouble with vector of pointers

Reply

Join Date: Feb 2008
Posts: 42
Reputation: klactose is an unknown quantity at this point 
Solved Threads: 1
klactose klactose is offline Offline
Light Poster

Trouble with vector of pointers

 
0
  #1
Jul 21st, 2008
Hello,

I'm having trouble compiling a header file for a class written in C++. What's so frustrating is that I can't see what I'm doing wrong. when I compile the code I get the the following 2 errors for lines 22 and 25:
expected ';' before '<' token
ISO C++ forbids declaration of 'vector' with no type


In line 22 I want to return a vector of pointers to a class Car. And in line 25 I am trying to declare a class member to be a vector of pointers to class Car. But I don't understand why I would place an extra ';' on those lines. And I thought that I was declaring vectors of Car* properly by using the syntax vector<Car*>.

Below is the entire header file can anyone spot what I am doing wrong?

  1. #ifndef PERSON_H
  2. #define PERSON_H
  3.  
  4. using std::string;
  5.  
  6.  
  7. class Car;
  8.  
  9. class Person {
  10. public:
  11. Person();
  12. Person(int ag, bool sx, string fn, string ln);
  13. ~Person();
  14. int getAge();
  15. std::string getName();
  16. std::string getFname();
  17. std::string getLname();
  18. bool getSex();
  19. void buyCar(string personName, int year, string carModel);
  20. Car* sellCar(Car *carPtr);
  21. void listCars();
  22. vector<Car*> getCars();
  23.  
  24. private:
  25. vector<Car*> carsOwned;
  26. int age;
  27. std::string fname;
  28. std::string lname;
  29. bool sex; // true = femaie, false = male
  30.  
  31. };
  32.  
  33. #endif
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Trouble with vector of pointers

 
0
  #2
Jul 22nd, 2008
You haven't told the compiler what a vector is, so it considers it an unknown type and goofs-up.

Either add a using std::vector; somewhere at the top, or say
std::vector <Car*> foo();

Hope this helps. (Isn't it annoying when the silly stuff gets you?)

[edit] BTW, the header file should also
#include <string>
#include <vector>
and not rely on the using program to do it for you.
Last edited by Duoas; Jul 22nd, 2008 at 12:05 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 42
Reputation: klactose is an unknown quantity at this point 
Solved Threads: 1
klactose klactose is offline Offline
Light Poster

Re: Trouble with vector of pointers

 
0
  #3
Jul 22nd, 2008
Thank you Duoas,

I guess I keep getting confused about where to add the includes. For some reason I thought I was supposed to be adding them to the .cpp files instead of the header files.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Trouble with vector of pointers

 
0
  #4
Jul 22nd, 2008
Some notes:
1. Use const std::string& parameters where possible (everywhere in this class). Return const std::string& from getLname and getFname members .
2. Car* sellCar(Car *carPtr); - extremelly unsafe member. You made car list (vector) private (that's OK) but allow user to sell any Car from the outer world via pointer. Return const vector<Car*>& from getCars.
3. female == true, I agree. But this fact may be not so evident for tired out programmers. Better add enum Sex { Male, Female }; in the class declaration.
4. Add const modifier to all proper member functions, use inline definition where possible. For example:
  1. int getAge() const { return age; }
  2. const std::string& getFname() const { return fname; }
5. What's personName parameter in buyCar member? Consider a possible use case for the Person class: we have person1 and person2, person1 sell a car to person2. You may get Car* pointer from person1 but where is arguments for person2.buyCar() in that case?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1300 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC