我的程序中有一个错误 'count'不能用作函数。
它指向以下行
mycount = (int) count (t1q1.begin(), t1q1.end(), 1);
我已经使用命名空间std包含了算法,但仍显示错误。
这就是我对矢量t1q1的处理方式 下面的代码允许程序读入文件并提取某些信息并将其推回到向量
我的功能是阅读文件并将其存储在各种变量中
void checkForAnswers(char *ptr)
{
char String[256];
int count = 0;
while ( *ptr != ':')
{
String[count] = *ptr;
ptr++;
count++;
}
String[count] = '\0';
//if topic is 1.
if (strcmp(String, "1") == 0)
{
currentPtr=Travels(':',startPtr);
int answerone1 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answertwo2 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerthree3 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfour4 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfive5 = (int)atoi(currentPtr);
t1q1.push_back(answerone1);
t1q2.push_back(answertwo2);
t1q3.push_back(answerthree3);
t1q4.push_back(answerfour4);
t1q5.push_back(answerfive5);
}
if (strcmp(String, "2") == 0)
{
currentPtr=Travels(':',startPtr);
int answerone1 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answertwo2 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerthree3 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfour4 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfive5 = (int)atoi(currentPtr);
t2q1.push_back(answerone1);
t2q2.push_back(answertwo2);
t2q3.push_back(answerthree3);
t2q4.push_back(answerfour4);
t2q5.push_back(answerfive5);
}
if (strcmp(String, "3") == 0)
{
currentPtr=Travels(':',startPtr);
int answerone1 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answertwo2 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerthree3 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfour4 = (int)atoi(currentPtr);
currentPtr=Travels(':',currentPtr);
int answerfive5 = (int)atoi(currentPtr);
t3q1.push_back(answerone1);
t3q2.push_back(answertwo2);
t3q3.push_back(answerthree3);
t3q4.push_back(answerfour4);
t3q5.push_back(answerfive5);
}
currentPtr=0;
}
MY MAIN
char Lines1[256];
int count1 = 0;
ifstream resultfile("results.txt");
if (resultfile.is_open())
{
do
{
resultfile.getline(Lines1, 256);
startPtr = Lines1;
checkForAnswers(startPtr);
count1++;
}while(resultfile.eof() == false);
resultfile.close();
}
int mycount;
cout<< "\n";
cout<< "TABULATION RESULTS\n";
cout<< "------------------\n";
cout<< "Topic 1, Question 1\n";
mycount = (int) count (t1q1.begin(), t1q1.end(), 1);
cout<< "Option 1: "<< mycount << " time(s)\n";
希望你们能够帮助我。 在此先感谢帮助的人! :)
答案 0 :(得分:3)
在您的代码count
中引用局部变量count
,其类型为int
,因此不能用作函数。您应该将变量命名为其他名称,或者使用其限定名称std::count
来引用该函数。
还要确保你#include <algorithm>
(并且如果你去重命名变量并使用其非限定名称调用函数,则有一个适当的using
声明。