标题:关于编译错误
只看楼主
izhu
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2012-12-2
结帖率:100%
已结贴  问题点数:20 回复次数:2 
关于编译错误
小弟初学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报错了。。。
搜索更多相关主题的帖子: void 源文件 
2013-02-02 17:48
fanpengpeng
Rank: 8Rank: 8
来 自:南极洲
等 级:蝙蝠侠
威 望:7
帖 子:299
专家分:849
注 册:2013-2-1
得分:20 
两个错误,都在头文件里:
1. 类定义{ }后要加;号
2. 类体中 public后的:号为中文字符,要改成英文字符
都是标点符号
加油!

人生是一场错过 愿你别蹉跎
2013-02-02 18:10
izhu
Rank: 1
等 级:新手上路
帖 子:7
专家分:5
注 册:2012-12-2
得分:0 
回复 2楼 fanpengpeng
嗯嗯,太感谢了,哈哈
2013-02-02 19:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-398107-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054210 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved