尝试编译代码时,每次都会出现以下错误。 这些是错误:
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
这是我程序的精简版。
triangle.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);
};
删除评论部分时,它会给我带来更多错误。 main.cpp中
#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;
}
任何想法可能出错?
答案 0 :(得分: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)
{
}