2011-09-14 5 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

워 : 그의 반환의 끝에 세미콜론을 잊어해야한다, 그래서 그의'trashme' 기능, 멤버 함수, 내가 장님 –

+0

내 대답은 죽은 것이 잘못되었습니다. –

3

fooTestClass의 하위 클래스이며, 당신이 그들을 평가해야하므로 trashmeTestClass의 멤버 함수입니다 :

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 

} 
관련 문제