首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有人能看看我的代码吗?错误C2059:语法错误:“公共”

有人能看看我的代码吗?错误C2059:语法错误:“公共”
EN

Stack Overflow用户
提问于 2014-11-13 05:11:23
回答 3查看 2.7K关注 0票数 0

我在写代数树程序。在编译过程中,我犯了很多错误。我不知道这些错误是从哪里来的。

这是我的代码:

代码语言:javascript
复制
//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

在编译过程中,我得到了以下错误:

代码语言:javascript
复制
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'

有人能告诉我我哪里错了吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-13 05:15:22

从替换开始

代码语言:javascript
复制
#include "Term.h";

通过

代码语言:javascript
复制
#include "Term.h"

(可能还有更多的错误)。实际上,第一条错误消息确切地告诉你,预处理器期待一个新行,在这里你写了一个分号,所以下次请先阅读错误信息。

票数 0
EN

Stack Overflow用户

发布于 2014-11-13 05:46:45

多件事。

为什么要在头文件中提供方法的定义?

代码语言:javascript
复制
//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文件被编译了吗?为什么?为什么不行?

如果你能回答这些问题,你就有了答案。这更像是家庭作业。请您只张贴相关的&只有那些问题,您需要技术援助。

票数 0
EN

Stack Overflow用户

发布于 2014-11-13 05:14:00

您缺少了虚拟字符串symbolicEval()函数的大括号。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26901765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档