关于编译错误
小弟初学C++,不知道该怎么编译有多个源文件的程序,所以上来求教大家,希望大家可以帮忙看看,vs报错是这样的:main.cpp
1>e:\程序\cpp源\constructor and destructor\constructor and destructor\main.cpp(9): error C2628: “CreatAndDestory”后面接“void”是非法的(是否忘记了“;”?)
1>e:\程序\cpp源\constructor and destructor\constructor and destructor\main.cpp(32): error C2556: “void my_creat(void)”: 重载函数与“CreatAndDestory my_creat(void)”只是在返回类型上不同
1> e:\程序\cpp源\constructor and destructor\constructor and destructor\main.cpp(9) : 参见“my_creat”的声明
1>e:\程序\cpp源\constructor and destructor\constructor and destructor\main.cpp(32): error C2371: “my_creat”: 重定义;不同的基类型
1> e:\程序\cpp源\constructor and destructor\constructor and destructor\main.cpp(9) : 参见“my_creat”的声明
1>e:\程序\cpp源\constructor and destructor\constructor and destructor\creat.cpp(11): error C2533: “CreatAndDestory::{ctor}”: 构造函数不能有返回类型
下面是我的代码:
头文件:
//creat.h
//Definition of class Creat And Destory.
//Member function defined in creat.cpp
#ifndef CREAT_H
#define CREAT_H
class CreatAndDestory
{
public:
CreatAndDestory( int ); //constructor
~CreatAndDestory(); //destructor
private:
int data;
}//end class CreatAndDestory
#endif
类cpp源文件:
//creat.cpp
//Member function definitions for class CreatAndDestory
# include <iostream>
using std :: cout;
using std :: endl;
# include "creat.h"
CreatAndDestory :: CreatAndDestory( int value )
{
data = value;
cout << "Object " << data << "constructor";
//every time when call constructor,output the infomation
}
CreatAndDestory :: ~CreatAndDestory()
{
cout << "Object " << data << "destructor" << endl;
//every time when call destructor,output the infomation
}
主函数:
//Demostrating for the order in which constructors and destructor are called
#include <iostream>
using std :: cout;
using std :: endl;
#include "creat.h"
void my_creat( void );
CreatAndDestory first( 1 ); //global object, when it is created, constructor is called
//and infomation is shown on the screan : "Object 1 constructor"
int main(void)
{
cout << " ( global created before main )" << endl;
CreatAndDestory second( 2 ); //local object,and infomation is shown
cout << " ( local automatic in main )" << endl;
static CreatAndDestory third( 3 );
cout << " ( local static in main )" << endl;
my_creat();
CreatAndDestory fourth( 4 );
cout << " ( local automatic in main )" << endl;
return 0;
}
void my_creat( void )
{
CreatAndDestory fifth( 5 );
cout << " ( local automatic in creat() )" << endl;
static CreatAndDestory sixth( 6 );
cout << " ( local static in creat() )" << endl;
CreatAndDestory seventh( 7 );
cout << " ( local automatic in creat() )" << endl;
}
然后我是把主函数和creat.cpp添加到VS新建的解决方案里的源文件里,creat.h放到解决方案里的头文件里,然后编译,然后vs报错了。。。