【问题标题】:Simple compile error but doesn't make sense [closed]简单的编译错误但没有意义[关闭]
【发布时间】:2016-05-02 17:22:30
【问题描述】:

这里是由于我的错误而使用的所有 .cpp 和 .h 文件。它们是我以前见过并修复过的错误,但我不明白为什么会出现这些错误,它不会为代码生成正确的语法。

形状.h

#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
#include <math.h>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stdlib.h>

using std::cout; using std::cerr; using std::endl;
using std::string;
    class Shape{
      public:
    Shape(const string&)  { } //default constructor
    virtual void print() const;
    virtual double get_area() const;
    virtual ~Shape();
      private:
    string color;
    };
#endif

形状.cpp

#include "shape.h"
using std::cout; using std::cerr; using std::endl;
using std::string;
void Shape::print() const{
cout << color << endl; }
Shape::Shape(const string& color1):color(color1)
{/*color = color1;*/ }
Shape::~Shape()
{ }

圆.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include <math.h>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include "shape.h"
using namespace std;
using std::cout; using std::cerr; using std::endl;
using std::string; using std::vector;
class Circle : public Shape{
  public:
  Circle(const string& , int); 
virtual void print() const;
virtual double get_area() const;
  private:
int radius;
};
#endif

circle.cpp

 #include "circle.h"
using std::cout; using std::cerr; using std::endl;
using std::string;
Circle::Circle(const string& color1,int newRadius):Shape(color1),radius(newRadius) {}

double Circle::get_area() const{
double area;
area = M_PI * (pow(radius,2) );
return area;
}

void Circle::print() const{
cout << "Circle: " <<
     << color << " circle, "
     << "radius " << radius << ", "
     << "area " << get_area() << endl;
}

主要

#include <iostream>
#include "shape.h"
#include "circle.h"
#include <vector>
using std::vector; using std::string;
using namespace std;
int main{

    vector<Shape *> shape1(1);
    shape1[0] = new Circle("Green",20);

/*
for(int i = 0; i < arr; i++ )
i.print();

for(int i = 0; i < arr; i++ )
delete [] arr;
*/
 return 0;

错误:

    assign9.cpp:9:17: error: expected primary-expression before ‘shape1’
 vector<Shape *> shape1(1);
                 ^
assign9.cpp:9:17: error: expected ‘}’ before ‘shape1’
assign9.cpp:9:17: error: expected ‘,’ or ‘;’ before ‘shape1’
assign9.cpp:10:1: error: ‘shape1’ does not name a type
 shape1[0] = new Circle("Green",20);
 ^
assign9.cpp:19:2: error: expected unqualified-id before ‘return’
  return 0;
  ^
assign9.cpp:20:1: error: expected declaration before ‘}’ token
 }

^

我不明白这些错误是如何产生的,甚至我的助教也无法弄清楚。我很感激任何反馈。谢谢

【问题讨论】:

  • 看起来您缺少一些标题包含。
  • 你添加了“使用标准;”指示?如果您使用未指定 std:: 的字符串和向量,则必须这样做。
  • 错字。你有class Cirlce 而不是class Circlelc 被错误地交换了。

标签: c++ class hierarchy


【解决方案1】:

圆而不是圆。

字符串而不是 std::string。

附带说明,Circle 的构造函数最好是 Circle::Circle(const string& color1, int newRadius):Shape(color1), radius(newRadius) {}

(在构造函数中而不是在括号中初始化所有内容)。

【讨论】:

  • 术语注释:如果你说“在构造函数中”,更好的措辞是“在成员初始化列表中”。
  • 感谢旁注,我修复了 Circle 而不是 Circle,但发生了完全相同的错误。
  • 我试过了。您的代码有很多错误,但不会发生同样的错误。也许你忘了保存文件。如果仍然出现,请创建最小示例(尝试在出现问题的地方创建一个文件,将所有文件组合在一起;然后使文件尽可能小,删除所有不必要的错误重现)。
  • 我不明白你的意思。我也在用我的终端来做这个
  • 你还有同样的错误吗?
猜你喜欢
  • 2021-05-21
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多