困惑为什么我收到C1010错误?

时间:2010-05-12 23:07:27

标签: c++

我有三个文件: 在http://forums.devarticles.com/c-c-help-52/confused-why-i-am-getting-c2143-and-c1010-error-259574.html

可以看到Main,slist.h和slist.cpp

我正在尝试创建一个程序,其中main从文件(roster.txt)中读取学生姓名列表,并按升序将所有名称插入列表中。这是完整的班级名单(notCheckedIN)。从这里我将阅读所有来参加考试的学生,每个签到的人都会将他们的名字转移到另一个名单上(按升序排列),称为现在。

最终产品是 notCheckedIN将包含所有未编写考试的学生的列表 present将包含所有编写考试的学生的列表

主档案:

// Exam.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "iostream"
#include "iomanip"
#include "fstream"
#include "string"
#include "slist.h"

using namespace std;

void OpenFile(ifstream&);
void GetClassRoster(SortList&, ifstream&);
void InputStuName(SortList&, SortList&);
void UpdateList(SortList&, SortList&, string);
void Print(SortList&, SortList&);
const string END_DATA = "EndData";

int main()
{

ifstream roster;
SortList notCheckedIn; //students present
SortList present; //student absent

OpenFile(roster);
if(!roster) //Make sure file is opened
return 1;
GetClassRoster(notCheckedIn, roster); //insert the roster list into the notCheckedIn list
InputStuName(present, notCheckedIn);
Print(present, notCheckedIn);
return 0;
}

void OpenFile(ifstream& roster)
//Precondition: roster is pointing to file containing student anmes
//Postcondition:IF file does not exist -> exit
{
string fileName = "roster.txt";
roster.open(fileName.c_str());

if(!roster)
cout << "***ERROR CANNOT OPEN FILE :"<< fileName << "***" << endl;
}
void GetClassRoster(SortList& notCheckedIN, ifstream& roster)
//Precondition:roster points to file containing list of student last name
// && notCheckedIN is empty
//Postcondition:notCheckedIN is filled with the names taken from roster.txt in ascending order
{
string name;
roster >> name;
while(roster)
{
notCheckedIN.Insert(name);
roster >> name;
}
}
void InputStuName(SortList& present, SortList& notCheckedIN)
//Precondition: present list is empty initially and notCheckedIN list is full
//Postcondition: repeated prompting to enter stuName
// && notCheckedIN will delete all names found in present
// && present will contain names present
// && names not found in notCheckedIN will report Error
{
string stuName;

cout << "Enter last name (Enter EndData if none to Enter): ";
cin >> stuName;
while(stuName!=END_DATA)
{
UpdateList(present, notCheckedIN, stuName);
}
}
void UpdateList(SortList& present, SortList& notCheckedIN, string stuName)
//Precondition:stuName is assigned
//Postcondition:IF stuName is present, stuName is inserted in present list
// && stuName is removed from the notCheckedIN list
// ELSE stuName does not exist
{
if(notCheckedIN.isPresent(stuName))
{
present.Insert(stuName);
notCheckedIN.Delete(stuName);
}
else
cout << "NAME IS NOT PRESENT" << endl;
}
void Print(SortList& present, SortList& notCheckedIN)
//Precondition: present and notCheckedIN contains a list of student Names present/not present
//Postcondition: content of present and notCheckedIN is printed
{
cout << "Candidates Present" << endl;
present.Print();

cout << "Candidates Absent" << endl;
notCheckedIN.Print();
}

标题文件:

//Specification File: slist.h
//This file gives the specifications of a list abstract data type
//List items inserted will be in order
//Class SortList, structured type used to represent an ADT
using namespace std;
const int MAX_LENGTH = 200;
typedef string ItemType;
//Class Object (class instance) SortList. Variable of class type.
class SortList
{
//Class Member - components of a class, can be either data or functions
public:
//Constructor
//Post-condition: Empty list is created
SortList();

//Const member function. Compiler error occurs if any statement within tries to modify a private data
bool isEmpty() const;
//Post-condition: == true if list is empty
// == false if list is not empty

bool isFull() const;
//Post-condition: == true if list is full
// == false if list is full

int Length() const;
//Post-condition: size of list

void Insert(ItemType item);
//Precondition: NOT isFull() && item is assigned
//Postcondition: item is in list && Length() = Length()@entry + 1

void Delete(ItemType item);
//Precondition: NOT isEmpty() && item is assigned
//Postcondition:
// IF items is in list at entry
// first occurance of item in list is removed
// && Length() = Length()@entry -1;
// ELSE
// list is not changed

bool isPresent(ItemType item) const;
//Precondition: item is assigned
//Postcondition: == true if item is present in list
// == false if item is not present in list

void Print() const;
//Postcondition: All component of list have been output
private:
int length;
ItemType data[MAX_LENGTH];
void BinSearch(ItemType, bool&, int&) const;

};

源文件:

//Implementation File: slist.cpp
//This file gives the specifications of a list abstract data type
//List items inserted will be in order
//Class SortList, structured type used to represent an ADT
#include "iostream"
#include "slist.h"

using namespace std;
// int length;
// ItemType data[MAX_SIZE];
//Class Object (class instance) SortList. Variable of class type.
SortList::SortList()
//Constructor
//Post-condition: Empty list is created
{
length=0;
}

//Const member function. Compiler error occurs if any statement within tries to modify a private data
bool SortList::isEmpty() const
//Post-condition: == true if list is empty
// == false if list is not empty
{
return(length==0);
}

bool SortList::isFull() const
//Post-condition: == true if list is full
// == false if list is full
{
return (length==(MAX_LENGTH-1));
}

int SortList::Length() const
//Post-condition: size of list
{
return length;
}

void SortList::Insert(ItemType item)
//Precondition: NOT isFull() && item is assigned
//Postcondition: item is in list && Length() = Length()@entry + 1
// && list componenet are in ascending order of value
{
int index;
index = length -1;
while(index >=0 && item<data[index])
{
data[index+1]=data[index];
index--;
}
data[index+1]=item;
length++;
}

void SortList:elete(ItemType item)
//Precondition: NOT isEmpty() && item is assigned
//Postcondition:
// IF items is in list at entry
// first occurance of item in list is removed
// && Length() = Length()@entry -1;
// && list components are in ascending order
// ELSE data array is unchanged
{
bool found;
int position;

BinSearch(item,found,position);

if (found)
{
for(int index = position; index < length; index++)
data[index]=data[index+1];
length--;
}
}

bool SortList::isPresent(ItemType item) const
//Precondition: item is assigned && length <= MAX_LENGTH && items are in ascending order
//Postcondition: true if item is found in the list
// false if item is not found in the list
{
bool found;
int position;

BinSearch(item,found,position);
return (found);
}

void SortList::Print() const
//Postcondition: All component of list have been output
{
for(int x= 0; x<length; x++)
cout << data[x] << endl;
}

void SortList::BinSearch(ItemType item, bool found, int position) const
//Precondition: item contains item to be found
// && item in the list is an ascending order
//Postcondition: IF item is in list, position is returned
// ELSE item does not exist in the list
{
int first = 0;
int last = length -1;
int middle;

found = false;
while(!found)
{
middle = (first+last)/2;
if(data[middle]<item)
first = middle+1;
else if (data[middle] > item)
last = middle -1;
else
found = true;
}
if(found)
position = middle;
}

我无法摆脱C1010错误: 致命错误C1010:查找预编译头时意外结束文件。您是否忘记在源代码中添加“#include”stdafx.h“'?

有没有办法摆脱这个错误? 当我包含“stdafx.h”时,我收到了以下32个错误(这对我来说没有意义,为什么因为我回过头来讨论如何使用Class方法 - 所有东西看起来都是a.ok。)

Error 1 error C2871: 'std' : a namespace with this name does not exist c:\..\slist.h 6
Error 2 error C2146: syntax error : missing ';' before identifier 'ItemType' c:\..\slist.h 8
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\..\slist.h 8
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\..\slist.h 8
Error 5 error C2061: syntax error : identifier 'ItemType' c:\..\slist.h 30
Error 6 error C2061: syntax error : identifier 'ItemType' c:\..\slist.h 34
Error 7 error C2061: syntax error : identifier 'ItemType' c:\..\slist.h 43
Error 8 error C2146: syntax error : missing ';' before identifier 'data' c:\..\slist.h 52
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\..\slist.h 52
Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\..\slist.h 52
Error 11 error C2061: syntax error : identifier 'ItemType' c:\..\slist.h 53
Error 12 error C2146: syntax error : missing ')' before identifier 'item' c:\..\slist.cpp 41
Error 13 error C2761: 'void SortList::Insert(void)' : member function redeclaration not allowed c:\..\slist.cpp 41
Error 14 error C2059: syntax error : ')' c:\..\slist.cpp 41
Error 15 error C2143: syntax error : missing ';' before '{' c:\..\slist.cpp 45
Error 16 error C2447: '{' : missing function header (old-style formal list?) c:\..\slist.cpp 45
Error 17 error C2146: syntax error : missing ')' before identifier 'item' c:\..\slist.cpp 57
Error 18 error C2761: 'void SortList:elete(void)' : member function redeclaration not allowed c:\..\slist.cpp 57
Error 19 error C2059: syntax error : ')' c:\..\slist.cpp 57
Error 20 error C2143: syntax error : missing ';' before '{' c:\..\slist.cpp 65
Error 21 error C2447: '{' : missing function header (old-style formal list?) c:\..\slist.cpp 65
Error 22 error C2146: syntax error : missing ')' before identifier 'item' c:\..\slist.cpp 79
Error 23 error C2761: 'bool SortList::isPresent(void) const' : member function redeclaration not allowed c:\..\slist.cpp 79
Error 24 error C2059: syntax error : ')' c:\..\slist.cpp 79
Error 25 error C2143: syntax error : missing ';' before '{' c:\..\slist.cpp 83
Error 26 error C2447: '{' : missing function header (old-style formal list?) c:\..\slist.cpp 83
Error 27 error C2065: 'data' : undeclared identifier c:\..\slist.cpp 95
Error 28 error C2146: syntax error : missing ')' before identifier 'item' c:\..\slist.cpp 98
Error 29 error C2761: 'void SortList::BinSearch(void) const' : member function redeclaration not allowed c:\..\slist.cpp 98
Error 30 error C2059: syntax error : ')' c:\..\slist.cpp 98
Error 31 error C2143: syntax error : missing ';' before '{' c:\..\slist.cpp 103
Error 32 error C2447: '{' : missing function header (old-style formal list?) c:\..\slist.cpp 103

3 个答案:

答案 0 :(得分:1)

您收到错误是因为您的项目配置为使用名为stdafx.h的预编译头(这是新的非空VC ++项目的默认设置)。有关如何打开或关闭该指令的说明,请参阅here(在“在Visual Studio开发环境中设置此编译器选项”下)。

至于你得到的错误 - 好吧,对于一个,你在头文件中是using namespace std,但它不包括任何定义该命名空间的任何标题。例如,您应该#include标题中使用的所有类型的标准标题 - <string>

哦,它应该是#include <iostream>(对于所有其他标准标题也是如此),而不是"iostream"。后者通常会起作用,但它是实现定义的,大多数实现都将其视为“首先查看当前目录”。

答案 1 :(得分:0)

我建议废弃您的代码并重新开始。

以下是建议:
1。使用标准库函数和结构。
不要在它们周围写包装。 例如,不需要OpenFile方法。它只是让程序更难阅读,浪费你的时间,惹恼了猪

<强> 2。使用尖括号,&lt; &gt;,围绕标准标题。
尖括号告诉编译器首先在“系统”或编译器目录中搜索。

第3。读取数据然后排序。
将所有数据读入{标准}容器,然后对容器进行排序。在每一行之后进行排序会浪费计算机时间,而会使猪惹恼

<强> 4。首选std函数前缀为std
例如,std::cout而不是cout。啊,但你说“我可以只包括using namespace std;”。是的,但这打开了std命名空间中所有内容的大门。另一个缓解的解决方案是使用命名空间标识每个std函数:using std::cout。保持名称空间标识很小。

<强> 5。首选EXIT_SUCCESS和EXIT_FAILURE。
main返回的标准值为EXIT_SUCCESSEXIT_FAILURE。这些在<cstdlib>中定义。

<强> 6。将流传递给对象的Print方法 这将允许您将输出发送到文件或控制台,而无需更改对象的代码。一个很好的例子是跟踪输出。将输出定向到文件,使其不会消失(如写入控制台)。

<强> 7。学习使用迭代器和算法
标准容器支持迭代器。您可以使用std::list和迭代器轻松打印std::vectorstd::copy的内容。 std::sort算法可以通过使用迭代器应用于容器(除了那些已经有排序方法的容器)。

<强> 8。保持简单
让您的程序尽可能简单,但并不简单。简单的程序比大型和复杂的程序具有更少的错误和更快的运行速度(并且它们更难以惹恼猪)。


“永远不要教猪唱歌,这会浪费你的时间并惹恼猪。” - 老笑话
“一切都应该尽可能简单,但不能简单。” - 解读阿尔伯特爱因斯坦。

答案 2 :(得分:0)

First Things First

作为一般建议,请始终尝试重复使用what you already have了解。 STL rich tools来解决这类问题。无情地重用,窃取代码,就像没有明天一样。你会以这种方式获得更快,更好,更强,更性感的结果。

快速解决方案

如果您和您只编写了名册文件,那么您可以保证该文件具有始终排序的名称列表。如果您可以依赖此保证,那么您可以使您的代码更快更轻。您不需要先在内存中加载此文件,也不需要对内存中的列表进行排序:

#include <iostream> // fstream,iterator,algorithm,string,vector

int main(int argc, char* argv[])
{
    typedef std::vector<std::string> lastnames;
    lastnames present;
    std::copy(istream_iterator<std::string>(std::cin),
              istream_iterator<std::string>(),
              std::back_inserter(present));
    std::sort(present.begin(), present.end());
    lastnames absent;
    std::ifstream roster("roster.txt");
    std::set_difference(std::istream_iterator<std::string>(roster),
                        std::istream_iterator<std::string>(),
                        present.begin(), present.end(),
                        std::back_inserter(absent));
    return 0;
}

确定的解决方案

如果您不能依赖名单文件进行排序,那么您必须对其进行排序,这意味着您必须将其加载到内存中。

    // ...
    std::ifstream roster("roster.txt");
    std::copy(std::istream_iterator<std::string>(roster),
              std::istream_iterator<std::string>(),
              std::back_inserter(absent));
    std::sort(absent.begin(), absent.end());
    lastnames::iterator e(std::set_difference(absent.begin(), absent.end(),
                                              present.begin(), present.end(),
                                              absent.begin()));
    absent.erase(e, absent.end());
    return 0;
}

看一看结果

您可以显示已登记但未出现(或缺席)学生的结果列表,如下所示:

cout << "Checked in, not present:\n";
copy(absent.begin(), absent.end(),
     ostream_iterator<string>(cout, "\n"));