C++ 矢量与频率搜索

C++ 矢量与频率搜索,c++,vector,binary,C++,Vector,Binary,我正在创建一个程序,在现有文件中记录一个数字的频率。我的名字是test,为什么它说“test”没有定义 这可能是我错过的一些小东西 #include <ostream> #include <iostream> #include <vector> #include <fstream> #include <string> using namespace std; int main() { string fileName; int aTe

我正在创建一个程序,在现有文件中记录一个数字的频率。我的名字是test,为什么它说“test”没有定义

这可能是我错过的一些小东西

#include <ostream>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;


int main()
{
string fileName;
int aTest;

cout << "Enter a File Name:";
cin >>fileName;

ifstream inFile (fileName.c_str());
if (! inFile)
{
cout << "!!Error in opening file 'test.dat'"<< endl;
    }

while( inFile >> aTest)

    vector <int> test (101, 0)

    test[aTest]++;

system("pause");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文件名;
国际测试;
cout>文件名;
ifstream infle(fileName.c_str());
如果(!infle)
{
库特(阿泰斯特)
病媒测试(101,0)
测试[aTest]++;
系统(“暂停”);
返回0;
}

您应该在
while
循环之外定义向量,并且应该添加适当的
{}
以使逻辑正确

尝试:


您有一个语法错误,
test
应该在循环之外定义

  vector<int> test(101, 0); // Removed whitespace and added semi-colon.
  while(inFile >> aTest) {  // Use braces for a new block.
    test[aTest]++;
  }
向量测试(101,0);//删除空格并添加分号。
而(infle>>aTest){//为新块使用大括号。
测试[aTest]++;
}

@sabrinafolzerati:不要使用
名称空间…
这一点很好。可以使用
使用std::string;
然后使用string或vector等等,只要您使用
语句提供单个
  #include <ostream> //^^remove one of them, don't include unnecessary headers
  #include <iostream>
  vector<int> test(101, 0); // Removed whitespace and added semi-colon.
  while(inFile >> aTest) {  // Use braces for a new block.
    test[aTest]++;
  }