在C ++中使用main之后添加其他函数时未声明的标识符?

时间:2012-05-02 16:02:20

标签: c++ arrays function const

#include "stdafx.h"
#include <iomanip>
#include <ostream>
#include <fstream>
using namespace std;

void FillArray(int x[ ], const int Size);
void PrintArray(int x[ ], const int Size);

int main() 
{
    const int SizeArray = 10;
    int A[SizeArray] = {0};
    FillArray (A, SizeArray);
    PrintAray (A, SizeArray);

    return 0;
}
void FillArray (int x[ ], const int Size)
{
    for( int i = 0; i < Size; i++);
    {
        cout << endl << "enter an integer"; //cout undeclared here
        cin >> x[i]; //cin and i undeclared here
    }

“cout”,“cin”和“i”都会收到错误“error C2065: 'cin' : undeclared identifier”。我不知道如何修复它们。我必须有三个功能:Main,fill array和print array。感谢帮助。

3 个答案:

答案 0 :(得分:8)

  1. 您最有可能希望包含<ostream>,而不是<iostream>,因为其中包含cincout
  2. for循环结束时的分号结束循环中执行的语句。因此,您的代码块与该循环分开,i不再在范围内。
  3. 为了支持更有用的问答形式,将来请将您的代码作为文本发布,而不是截图。


    enter image description here

答案 1 :(得分:6)

1)对于include <iostream>cin的定义,您必须cout
2)你的for循环后你有一个分号,这将防止它重复。它也使i的范围结束,所以你也不能在括号中使用它。
3)没有充分理由不要使用using namespace

4)不要使用代码图片 5)始终提供完整的错误消息。在Visual Studio中,位于“输出窗口”而不是“错误窗口”。例如,“标识符未识别”不是错误消息 6)始终在发布之前将代码减少到SSCCE。 95%的情况下你会自己发现问题。

答案 2 :(得分:3)

在iostream中定义了

std::coutstd::cin,因此您必须在文件顶部添加#include<iostream>