2011-09-14 11 views
1

構造体を返す関数が必要です。だから、私のヘッダファイルで、構造体と関数のシグネチャを定義しました。私のコードファイルでは、私は実際の機能を持っています。私は "不明な型名"について間違いがあります。すべてはこれのための非常に標準的なフォーマットに従っているように見えます。関数から構造体を返すときに問題が発生する

これはなぜ機能しないのでしょうか?

TestClass.h

class TestClass { 
public: 

    struct foo{ 
     double a; 
     double b; 
    }; 

    foo trashme(int x); 

} 

TestClass.cpp

#include "testClass.h" 

foo trashme(int x){ 

    foo temp; 
    foo.a = x*2; 
    foo.b = x*3; 

    return(foo) 

} 

答えて

2

fooがグローバル名前空間にないため、trashme()が見つかりません。何が欲しいのはこれです:

TestClass::foo TestClass::trashme(int x){ //foo and trashme are inside of TestClass 

    TestClass::foo temp; //foo is inside of TestClass 
    temp.a = x*2; //note: temp, not foo 
    temp.b = x*3; //note: temp, not foo 

    return(temp) //note: temp, not foo 

} 
+0

私は盲目 –

+0

おっと答えは死んで間違っています。 –

+0

私だけど、それが修飾する必要がありますので、彼の 'trashme'関数は、メンバ関数である、と彼は彼のリターンの終わりにセミコロンを忘れてしまった:) –

3

fooTestClassの子クラスで、あなたがそれらを修飾する必要があるのでtrashmeは、TestClassのメンバ関数である:

TestClass::foo TestClass::trashme(int x){ 

    foo temp; // <-- you don't need to qualify it here, because you're in the member function scope 
    temp.a = x*2; // <-- and set the variable, not the class 
    temp.b = x*3; 

    return temp; // <-- and return the variable, not the class, with a semicolon at the end 
        // also, you don't need parentheses around the return expression 

} 
関連する問題