| | |
Trouble with vector of pointers
![]() |
•
•
Join Date: Feb 2008
Posts: 42
Reputation:
Solved Threads: 1
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?
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?
C++ Syntax (Toggle Plain Text)
#ifndef PERSON_H #define PERSON_H using std::string; class Car; class Person { public: Person(); Person(int ag, bool sx, string fn, string ln); ~Person(); int getAge(); std::string getName(); std::string getFname(); std::string getLname(); bool getSex(); void buyCar(string personName, int year, string carModel); Car* sellCar(Car *carPtr); void listCars(); vector<Car*> getCars(); private: vector<Car*> carsOwned; int age; std::string fname; std::string lname; bool sex; // true = femaie, false = male }; #endif
You haven't told the compiler what a vector is, so it considers it an unknown type and goofs-up.
Either add a
Hope this helps. (Isn't it annoying when the silly stuff gets you?)
[edit] BTW, the header file should also
and not rely on the using program to do it for you.
Either add a
using std::vector; somewhere at the top, or saystd::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.
Some notes:
1. Use
2.
3. female == true, I agree. But this fact may be not so evident for tired out programmers. Better add
4. Add const modifier to all proper member functions, use inline definition where possible. For example:
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?
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:
C++ Syntax (Toggle Plain Text)
int getAge() const { return age; } const std::string& getFname() const { return fname; }
![]() |
Similar Threads
- help:stl vector of user defined objects (C)
- Deleting Characters Function (C)
- please fix the error (C++)
- Trouble with Pointers and Arrays (C)
Other Threads in the C++ Forum
- Previous Thread: Default values for refrences
- Next Thread: Text Editor that saves automatically.
Views: 1300 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code command compile compiler console constructor conversion convert count delete dll dynamic encryption error file files filestream fstream function functions game givemetehcodez graph gui homework http iamthwee ifstream input int lazy linker list loop loops math matrix member memory multidimensional newbie number object objects opengl operator output parameter pointer pointers problem program programming project qt random read recursion recursive reference server simple sort spoonfeeding string strings struct student studio system template templates text time tree variable vc++ vector video visual visualstudio void win32 window windows winsock wordfrequency






