私は自分のクラスの中で単にベクターを使用しようとしています。ベクトルにアクセスしようとすると、定義されていないことがわかります(ただし、ヘッダーに定義しています)。カスタムクラス内にベクターを定義する
私はPersonとDogという2つのクラスを持っています。 1つの人が1つ以上の犬を所有することができるので、自分が所有する犬をアレイに追加したい。これは本当に簡単なので、この問題は本当に私に手をつけ始めているはずです。
クラス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
}
そして、ここでは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
あなたは私の犬の授業を見てしたい場合は、私はそれらを貼り付けます同様:
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;
}
と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
はまた、私は、これは何の宿題ではないことを追加します。私はjavaに慣れていて、将来の仕事のために必要とするので、C++を学ぶことのみを試みています。
EDIT:更新されたコード
あなたは 'namespace std'を使用していて、' vector'を 'std :: vector'として修飾しています。 'using'指示文を取り除くとどうなりますか? –
サイドノートとして:コンストラクタで[初期化リスト](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6)を使用する必要があります。 –
私はそれがエラーではないので、最初のエラーではないと賭けて、私はコンパイラが最初に表示される他のエラーを参照してください。最初のエラーを修正してください。このエラーは消えてしまいます。つまり、あなたのインクルードは間違った場所にあります。 –