これを行う正しい構文は何ですか?確かに私はいくつかの愚かな間違いをした...残念ながら、私はベクトルをよりよく理解しようとしています。私は不要なポインタを作成したことを知っていますが、構文を理解する必要があります。コンパイル時に私が取得オブジェクトの関数への戻りベクトル
#include <iostream>
#include <vector>
class otherClass
{
public:
otherClass(int x):value(x)
{
//ctor
}
int getValue()
{
return value;
}
private:
int value;
};
class MyClass
{
public:
MyClass(int x)
{
obj = new std::vector<otherClass>(x,otherClass{5});
}
otherClass getVector()
{
return obj; //HERE FIRST ERROR <---------------
}
private:
std::vector<otherClass>*obj;
};
void doSomething(otherClass*obj)
{
std::cout << obj->getValue() << std::endl;
}
int main()
{
MyClass*aClass = new MyClass(10);
doSomething(aClass->getVector()); //HERE SECOND ERROR <---------------
return 0;
}
エラー:まず
:セカンド
error: invalid conversion from 'std::vector<otherClass>*' to 'int' [-fpermissive]
:
error: cannot convert 'otherClass' to 'otherClass*' for argument '1' to 'void doSomething(otherClass*)'
'getVector'は' otherClass'ではなくベクトルを返すように聞こえます。 – aschepler
あなたは何をしようとしているのか明確ではありません。あなたは関数からベクトルを返そうとしていますか? – pm100
このプログラムは 'std :: cout << 5 << std :: endl;' – myaut