0

尝试编译我的代码时,我每次都会收到以下错误。这些是错误:

C:\...\main.cpp In file included from main.cpp
C:\...\triangle.h In constructor `trianglePlus::trianglePlus(double, double, double, char*)':
C:\...\triangle.h expected `{' at end of input
C:\...\Makefile.win [Build Error]  [main.o] Error 1 

这是我的程序的精简版。

三角形.h:

class triangle { 
  double x;
  double y;
  double z;

  public:
         triangle(double a,double b,double c);
  };
class trianglePlus: public triangle {
  char * name;
  public:
         trianglePlus(double a,double b,double c,char * v):triangle(a,b,c);
  };

删除评论部分时,它给了我更多错误。主文件

#include <cstdlib>
#include <iostream>
#include <math.h>
#include "triangle.h"
using namespace std;

triangle::triangle(double a, double b, double c) {
    x = a;
    y = b;
    z = c;
}

/*
trianglePlus::trianglePlus(double a,double b,double c,char * v):trijsturis(a,b,c) {
    x = a;
    y = b;
    z = c;
    name = new char[20];
    strcpy(name,v);
    }
*/

int main()
{
    system("PAUSE");
    return 0;
}

有什么想法可能是错的吗?

4

1 回答 1

7

您的构造函数需要完全定义或仅声明。由于要指定初始化列表,因此需要定义完整的构造函数。最好这样,在类定义中内联:

trianglePlus(double a, double b, double c, char * v)
: triangle(a,b,c)
, x(a)
, y(b)
, z(c)
{
}

或者,只需将声明放在标题中,并将定义放在源文件中:

trianglePlus(double a, double b, double c, char * v);

trianglePlus::trianglePlus(double a, double b, double c, char * v)
: triangle(a,b,c)
, x(a)
, y(b)
, z(c)
{
}
于 2012-11-10T18:11:08.650 回答