2012-07-03 4 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

Тип "а" в параметре функции? – phantasmagoria

+1

[Этот хороший набор учебников C++] (http://www.cplusplus.com/doc/tutorial/) может вам помочь. – Gigi

+3

почему так много downvotes? OP обеспечивает минимальный рабочий пример и сообщение об ошибке завершено. –

ответ

8

Вы забыли объявить тип a при определении print.

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; 
} 
Смежные вопросы