我在写代数树程序。在编译过程中,我犯了很多错误。我不知道这些错误是从哪里来的。
这是我的代码:
//file: Term.h
#ifndef TERM
#define TERM
#include <sstream>
#include <string>
using namespace std;
class Term {
public:
Term() {}
virtual ~Term() {}
virtual string symbolicEval() = 0;
virtual double numericalEval(double X) = 0;
};
#endif
//file: UnaryOp.h
#ifndef UNARYOP
#define UNARYOP
#include "Term.h";
class UnaryOp: public Term{
protected:
Term* Child;
public:
UnaryOp(Term* l){Child = l;};
virtual ~UnaryOp(){delete Child;};
virtual string symbolicEval(){};
virtual double numericalEval(){};
};
#endif UNARYOP
//file:CCos.h
#ifndef COS_H
#define COS_H
#include "UnaryOp.h"
class Cos: public UnaryOp{
public:
Cos(Term * l):UnaryOp(l){};
virtual ~ Cos(){};
virtual string symbolicEval(){
ostringstream oss;
oss << "cos(x)" << endl;
return oss.str();
};
virtual double numericalEval(double X){
return cos(Child->numericalEval(X));
}
}
#endif COS_H在编译过程中,我得到了以下错误:
1>c:\users\administrator\desktop\algebra\algebra\unaryop.h(3): warning C4067: unexpected tokens following preprocessor directive - expected a newline
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2236: unexpected 'class' 'Cos'. Did you forget a ';'?
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2143: syntax error : missing ';' before ':'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2059: syntax error : ':'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2059: syntax error : 'public'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2143: syntax error : missing ';' before '{'
1>c:\users\administrator\desktop\algebra\algebra\ccos.h(6): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\administrator\desktop\algebra\algebra\algebra.cpp(29): error C2061: syntax error : identifier 'Cos'有人能告诉我我哪里错了吗?
发布于 2014-11-13 05:15:22
从替换开始
#include "Term.h";通过
#include "Term.h"(可能还有更多的错误)。实际上,第一条错误消息确切地告诉你,预处理器期待一个新行,在这里你写了一个分号,所以下次请先阅读错误信息。
发布于 2014-11-13 05:46:45
多件事。
为什么要在头文件中提供方法的定义?
//file:CCos.h
#ifndef COS_H
#define COS_H
#include "UnaryOp.h"
class Cos: public UnaryOp{
public:
Cos(Term * l):UnaryOp(l){};
virtual ~ Cos(){};
virtual string symbolicEval(){
ostringstream oss;
oss << "cos(x)" << endl;
return oss.str();
};
virtual double numericalEval(double X){
return cos(Child->numericalEval(X));
}
}
#endif COS_H如果在".H“文件中使用方法声明后的"{}”,请解释一下这意味着什么?编译器是如何理解它的?
你什么时候用冒号?它是在方法声明之后,还是在定义之后,还是两者都有?
.h文件被编译了吗?为什么?为什么不行?
如果你能回答这些问题,你就有了答案。这更像是家庭作业。请您只张贴相关的&只有那些问题,您需要技术援助。
发布于 2014-11-13 05:14:00
您缺少了虚拟字符串symbolicEval()函数的大括号。
https://stackoverflow.com/questions/26901765
复制相似问题