2012-07-03 12 views
0

아주 간단한 프로그램입니다. 나는 함수를 위에서 정의하고 루프에서 함수를 호출하고있다 print.왜이 루프가 작동하지 않습니까?

하지만 다음과 같은 오류가 점점 오전 :

prog.cpp:5: error: variable or field ‘print’ declared void 
prog.cpp:5: error: ‘a’ was not declared in this scope 
prog.cpp: In function ‘int main()’: 
prog.cpp:11: error: ‘print’ was not declared in this scope 

를 여기있다 :

#include <iostream>  
using namespace std; 

void print(a) { 
    cout << a << endl; 
} 

int main() { 
    for (int i = 1; i <= 50; i++) { 
     if (i % 2 == 0) print(i); 
    } 

    return 0; 
} 
+3

"A"의 함수 매개 변수의 유형? – phantasmagoria

+1

[이 좋은 C++ 자습서 세트] (http://www.cplusplus.com/doc/tutorial/)가 도움이 될 수 있습니다. – Gigi

+3

왜 그렇게 많은 downvotes? OP는 최소한의 작업 예제를 제공하며 오류 메시지가 완료되었습니다. –

답변

8

print를 정의 할 때 당신은 a의 유형을 선언하는 것을 잊었다.

6

이 시도 :

void print(int a) { 
0

변화에 :

void print(int a) { // notice the int 
    cout << a << endl; 
} 
2

C++ 아무튼 동적 유형이있다. 따라서 "a"변수의 유형을 수동으로 지정하거나 함수 템플리트를 사용해야합니다.

void print(int a) { 
    cout << a << endl; 
} 

template <typename T> 
void print(T a) { 
    cout << a << endl; 
} 
0
#include <iostream> 

using namespace std; 

void print(int a) { 
    cout << a << endl; 
} 

int main() { 
    for (int i = 1; i <= 50; i++) { 
     if (i % 2 == 0) print(i); 
    } 

    return 0; 
} 
관련 문제