John Snow
John Snow

Reputation: 5344

Defining a vector within a custom class

I'm trying to simply use a vector within one of my classes. When trying to access the vector it tells me that it's undefined (but I've defined it in my header).

I have two classes, Person and Dog. A person can own one or more dogs so I want to add each dog a person owns into an array. This should be real simple so this problem is really starting to get to me. Here's some code:

The class Person.cpp:

#include "Person.h"
#include "stdafx.h"
#include <iostream>

using namespace std;

Person::Person(string name, string address, int age)
    :name(name),
    address(address),
    age(age)
    {}
int Person::getAge(){
    return age;
}
std::string Person::getDogInfo(int index){
}
void Person::addDog(string dogName, string breed){
    dogCollection.push_back(Dog(dogName, breed));
}
std::vector<Dog> getDogs(){
    return dogCollection;  //dogCollection undefined error here
}

And here's Person.h:

#ifndef Person_H
#define Person_H
#include <vector>
#include "Dog.h"
using namespace std;
class Person{
    public:
        Person(string name, string address, int age);
        string getName(){return name};
        string getAddress(){return address};
        void addDog(string dogName, string breed);
        string getDogInfo(int index);
        std::vector<Dog> getDogs();
        int getAge();

    private:
        string name;
        string address;
        int age;
        std::vector<Dog> dogCollection;
};
#endif

If you want to have a look at my dog classes I'll paste them as well:

Dog.cpp:

#include "stdafx.h"
#include <iostream>
#include "dog.h"

Dog::Dog(string dogName, string breed)
    :dogName(dogName),
        breed(breed){}

std::string Dog::Dog.getDogName(){
return dogName;
}

std::string Dog::Dog.getBreed(){
return breed;
}

and Dog.h:

#ifndef Dog_H
#define Dog_H
#include <iostream>
using namespace std;

class Dog{
public:
    Dog(std::string dogName, std::string breed);
    std::string getDogName();
    std::string getBreed();
private:
    std::string dogName;
    std::string breed;
};
#endif

Also, I just want to add that this is no homework. I'm used to java and I'm only trying to learn some C++ since I need it for future work.

EDIT: Updated the code

Upvotes: 1

Views: 6266

Answers (3)

Bo Persson
Bo Persson

Reputation: 92321

    std::vector<Dog> dogCollection;  // here im defining dogCollection, no error here! 

There actually is an problem here - class Dog isn't known to the compiler at this point.

You can solve this by either including Dog.h before Person.h in Person.cpp, or better add an #include "Dog.h" at the top of Person.h.

Upvotes: 2

hmjd
hmjd

Reputation: 122001

This is incorrect (and unrequired):

dogCollection = new std::vector<Dog>; // Remove this line.

as dogCollection is not a std::vector<Dog>*.


This is also incorrect:

void Person::addDog(string dogName, string breed){
    Dog *newDog = new Dog(dogName, breed);
    dogCollection.push_back(newDog);
}

as dogCollection contains Dog instances, not Dog*. Change to:

void Person::addDog(string dogName, string breed){
    dogCollection.push_back(Dog(dogName, breed));
}

There is a problem with all of constructors:

Person::Person(string name, string address, int age){
    name=name;
    address=address;
    age=age;
}

This is assigning the argument name to itself: it is not assigning to the member name. Same for address and age and similarly for the constructors of the other classes. Use initializer list:

Person::Person(string name, string address, int age) :
    name(name),
    address(address),
    age(age)
{}

This method does not return a std::string:

string Person::getDogInfo(int index){
}

EDIT:

Missing class qualifier:

std::vector<Dog> getDogs(){
    return dogCollection;  //dogCollection undefined error here
}

means this is just a free function, with no association to class Person and therefore no access to dogCollection.

Change to:

std::vector<Dog> Person::getDogs(){
    return dogCollection;
}

Upvotes: 1

aldo
aldo

Reputation: 2987

There are several problems with your code, and most of the other answers have pointed them out - mostly regarding the use of new when it should not be used. (Are you a C# programmer, moving to C++?)

However, there are problems with the #include directives as well. As @Bo mentioned, since Person uses Dog, you should include that header in Person.h. But Person also uses vector so that header should be included there as well. So Person.h should start with...

#include <vector>
#include "Dog.h"

Then in Person.cpp you don't have to include those files.

As a general rule (you can learn about "forward declaration" later), any types referenced in a header should be #included in that header.

Upvotes: 1

Related Questions