c++ - 简单的编译错误但没有意义

标签 c++ class hierarchy

<分区>

这是我的错误结果中使用的所有 .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

圆.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
 }

^

我不明白这些错误是怎么来的,连我的助教也弄不明白。我感谢任何反馈。谢谢

最佳答案

Cirlce 而不是 Circle。

字符串而不是 std::string。

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

(在构造函数中而不是在括号中初始化所有你能初始化的东西)。

关于c++ - 简单的编译错误但没有意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36988060/

相关文章:

c++ - 不是 'const' 的引用不能绑定(bind)到非左值

c++ - 有没有办法测试默认构造函数不存在?

c++ - 在 boost::asio::buffer 中使用像 std::vector<std::complex<double>> 这样的参数

c++ - C++中如何让类的成员函数每次调用时生成不同的随机数?

c++ - 3D 顶点类或结构

java - 从另一个子类访问子方法

php - 表示分层任务/子任务的最佳方式 (MySQL/PHP)

c++ - 如何在函数内部声明全局变量?

c++ - Swift 中的类

SQL - 如何存储和导航层次结构?