程序的目的是统计学生的平均成绩,头文件student_info.h中声明了相应的结构体和函数,问题是.h文件中都包含了#include <iostream>,为什么在相应的源文件中还会出现“ std:is not a class or namespace name”这样的错误。
以下是头文件:student_info.h
#ifdef GUARD_Student_info
#define GUARD_Student_info
#include <iostream>
#include <string>
#include <vector>
struct student_info
{
string name;
double midterm,final;
std::vector<double> homework;
};
bool compare(const student_info&, const student_info& );
std::istream& read(std::istream&, student_info&);
std::istream& read(std::istream&, std::vector<double>&);
#endif
以下是源文件“student_info.cpp”
#include "Student_info.h"
using namespace std;
bool compare(const student_info& x, const student_info& y)
{
return x.name<y.name;
}
istream& read(istream& is , student_info& s)
{
is>>s.name>>s.midterm>>s.final;
read_hw(is,s.homework);
return is;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if(in) {
hw.clear();
double x;
while(in >> x)
hw.push_back(x);
in.clear();
}
return in;
}
此外编译完后出现了30个错误,如下:
G:\project\StudentResult\student_info.cpp(4) : error C2871: 'std' : does not exist or is not a namespace
G:\project\StudentResult\student_info.cpp(6) : error C2143: syntax error : missing ',' before '&'
G:\project\StudentResult\student_info.cpp(6) : error C2059: syntax error : '&'
G:\project\StudentResult\student_info.cpp(9) : error C2065: 'x' : undeclared identifier
G:\project\StudentResult\student_info.cpp(9) : error C2228: left of '.name' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(9) : error C2065: 'y' : undeclared identifier
G:\project\StudentResult\student_info.cpp(9) : error C2228: left of '.name' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(12) : error C2143: syntax error : missing ';' before '&'
G:\project\StudentResult\student_info.cpp(12) : error C2501: 'istream' : missing storage-class or type specifiers
G:\project\StudentResult\student_info.cpp(12) : error C2061: syntax error : identifier 'istream'
G:\project\StudentResult\student_info.cpp(13) : error C2501: 'read' : missing storage-class or type specifiers
G:\project\StudentResult\student_info.cpp(14) : error C2065: 'is' : undeclared identifier
G:\project\StudentResult\student_info.cpp(14) : error C2065: 's' : undeclared identifier
G:\project\StudentResult\student_info.cpp(14) : error C2228: left of '.name' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(14) : error C2228: left of '.midterm' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(14) : error C2228: left of '.final' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(15) : error C2065: 'read_hw' : undeclared identifier
G:\project\StudentResult\student_info.cpp(15) : error C2228: left of '.homework' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(19) : error C2143: syntax error : missing ';' before '&'
G:\project\StudentResult\student_info.cpp(19) : error C2501: 'istream' : missing storage-class or type specifiers
G:\project\StudentResult\student_info.cpp(19) : error C2086: 'istream' : redefinition
G:\project\StudentResult\student_info.cpp(19) : error C2061: syntax error : identifier 'istream'
G:\project\StudentResult\student_info.cpp(20) : error C2501: 'read_hw' : missing storage-class or type specifiers
G:\project\StudentResult\student_info.cpp(20) : error C2373: 'read_hw' : redefinition; different type modifiers
G:\project\StudentResult\student_info.cpp(21) : error C2065: 'in' : undeclared identifier
G:\project\StudentResult\student_info.cpp(22) : error C2065: 'hw' : undeclared identifier
G:\project\StudentResult\student_info.cpp(22) : error C2228: left of '.clear' must have class/struct/union type
G:\project\StudentResult\student_info.cpp(25) : error C2296: '>>' : illegal, left operand has type 'double'
G:\project\StudentResult\student_info.cpp(25) : error C2297: '>>' : illegal, right operand has type 'double'
G:\project\StudentResult\student_info.cpp(25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
当我在源文件中加入"include<iostream>"后'std' : does not exist or is not a namespace的问题就解决了,可我还是不懂,请高人指点,谢谢!
这个#ifdef GUARD_Student_info,改成 #ifndef或 #pragma once