2014-02-22 2 views
0

나는 프로그래밍에 대한 지식이 부족하여 유감이므로 더 노력하고 쉽게 할 수 있다는 것을 알고 있습니다. printStructValue 함수를 호출하면 프로그램이 중단됩니다. 나는 내 사고 과정을 설명하기 위해 의견을 남겼습니다.구조체의 배열을 반환하는 함수를 만드는 방법

#include <iostream> 
#include <vector> 
using namespace std; 

struct selection //vector array to tell me what is selected. ex:'w',5 is wall 5 
{  
    char c; 
    int id; 
}; vector<selection> Sel(20,selection()); 

struct walls  //struct to hold wall data 
{ 
    int id; 
    int x,y,z; 
    int spriteState; 
}; walls W[10]; 

struct floors  //struct to hold floor data 
{ 
    int id; 
    int x,y,z; 
}; floors F[10]; 


template <typename T,typename U> 
T returnAnyArray(int st, T t,U u) //function that returns any type passed 
{ 
    if(st==1){t;} //if st==1, then return the first, walls W 
    if(st==2){u;} //if st==2, then return the second, floors F 
} 

template <typename T> 
void printStructValue(T t, int d) //print any struct value 
{ 
    cout<<"passed:"<<t[d].x<<endl; 
} 

int main() 
{ 
    W[7].x=204; //init value 
    F[7].x= 73; //init value 

    //what I would like to happen is... 
    printStructValue((returnAnyArray(1,W,F)),7); //W is returned and passed so W[7].x gets printed. 
    printStructValue((returnAnyArray(2,W,F)),7); //F is returned and passed so F[7].x gets printed. 

    system("pause"); 
} 
+0

C++로 시작한다면 템플릿은 쉬운 시작점이 아닙니다. 아마 당신은 벽과 바닥 사이에 공통의 기본 클래스를 가질 필요가 있을까요? 그러나 템플릿은 필요하지 않습니다. – mpromonet

답변

1

returnAnyArray 함수는 무엇을 반환해야하지만 유형도 일치해야합니다. 이

template<typename T, typename U> 
auto returnAnyArray(int st, T t, U u) -> decltype(st == 1 ? t : u) 
{ 
    return st == 1 ? t : u; 
} 
+0

아 좋아. 나는 이와 같은 것을 찾고있었습니다. 나는 그것을 시도했지만 오류가 발생했습니다 "예상 init - declarator before '->'토큰." 그것은 내 말에 구문 오류가 있습니까? – user3341184

+0

@ user3341184 아니요. C++ 11을 활성화하면됩니다. '-std = C++ 11 '옵션을 프로그램에 추가하십시오. – 0x499602D2

+0

@ user3341184 작동 했습니까? – 0x499602D2

0

템플릿 함수 returnAnyArray는 실제로 아무것도 반환하지 않습니다. 따라서 printStructValue가 가비지 포인터를 전달 중입니다. 나는 컴파일러가 이것을 잡아 내지 않고 경고 나 오류를 출력하지 않는다는 것에 놀랐다. 아마도 그것은 템플릿이기 때문입니다.

0

작동하지만 예상 한 것과 다를 수 있습니다.

#include <iostream> 
#include <vector> 
using namespace std; 

struct walls  //struct to hold wall data 
{ 
    int id; 
    int x,y,z; 
    int spriteState; 
}; 
walls W[10]; 

struct floors //struct to hold floor data 
{ 
    int id; 
    int x,y,z; 
}; 
floors F[10]; 

void printStructValue(walls * t, int d) //print any struct value 
{ 
    cout<<"passed:" << t[d].x<<endl; 
} 

void printStructValue(floors * t, int d) //print any struct value 
{ 
    cout<<"passed:"<< t[d].x<<endl; 
} 

int main() 
{ 
    W[7].x=204; //init value 
    F[7].x= 73; //init value 

    //what I would like to happen is... 
    printStructValue(W,7); //W is returned and passed so W[7].x gets printed. 
    printStructValue(F,7); //F is returned and passed so F[7].x gets printed. 

} 
+0

예, 이것에 대해 알고 있습니다. 하지만 선택 배열을 변경하면 printStructValue에 int 값 (1 또는 2)을 보내도록 선택을 통해 for 루프를 실행하려고합니다. – user3341184

0

가 그것에게 C++ 방법을 수행하십시오 :

물론
struct structuralMember { 
    virtual ~structualMember() { } 
    virtual void printme(std::ostream& out) = 0; 
}; 

struct walls : public structualMember {  
    int x; 
    void printme(std::ostream& out) { out << x; } 
    }; 

struct floors : public structuralMember { 
    int x; 
    void printme(std::ostream& out) { out << x; } 
    }; 

, 그 중 정말 정교한 아니지만, 그것이 시작이다.

관련 문제