鱼C论坛

 找回密码
 立即注册
查看: 1975|回复: 13

[已解决]关于类声明和成员函数定义的分离

[复制链接]
发表于 2016-8-12 22:12:30 | 显示全部楼层 |阅读模式
30鱼币
//student.h
#include<string>
using namespace std;
class student
{
public:
    void set_in();
    void display();
private:
    int num;
    string name;
    char sex;
};
//student.cpp
#include<iostream>
#include"student.h"
void student::set_in()
{
    cin>>num>>name>>sex;
}
void student::display()
{
    cout<<num<<name<<sex<<endl;
}


main.cpp
#include <iostream>
#include"student.h"

using namespace std;

int main()
{
    class student s;
    s.set_in();
    s.display();
    return 0;
}
照着C++课本学类声明与成员函数定义的分离,结果编译一直出错,请问哪位大神知道能否解答一下??用的软件是code blocks,


错误显示:undefined reference to 'student::set_in()'
                undefined reference to 'student::display()'
最佳答案
2016-8-12 22:12:31
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
        void set_in();
        void display();
private:
        int num;
        string name;
        char sex;
};



#include<iostream>
#include"Header.h"
void student::set_in()
{
        cin >> num >> name >> sex;
}
void student::display()
{
        cout << num << name << sex << endl;
}

#include <iostream>
#include"Header.h"

using namespace std;

int main()
{
        class student s;
        s.set_in();
        s.display();
        return 0;
}

最佳答案

查看完整内容

#include #include using namespace std; class student { public: void set_in(); void display(); private: int num; string name; char sex; }; #include #include"Header.h" void student::set_in() { cin >> num >> name >> sex; } void student::display() { cout
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-12 22:12:31 | 显示全部楼层    本楼为最佳答案   
#include<iostream>
#include<string>
using namespace std;
class student
{
public:
        void set_in();
        void display();
private:
        int num;
        string name;
        char sex;
};



#include<iostream>
#include"Header.h"
void student::set_in()
{
        cin >> num >> name >> sex;
}
void student::display()
{
        cout << num << name << sex << endl;
}

#include <iostream>
#include"Header.h"

using namespace std;

int main()
{
        class student s;
        s.set_in();
        s.display();
        return 0;
}

图片

图片
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-13 23:59:42 | 显示全部楼层
我编了一点问题都没,你怎么弄的
QQ图片20160814000021.png

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +2 收起 理由
liyuanjun + 5 + 5 + 2 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-14 09:55:01 | 显示全部楼层
Krant5 发表于 2016-8-13 23:59
我编了一点问题都没,你怎么弄的

请问你用的这个软件叫什么??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-14 10:48:36 | 显示全部楼层
本帖最后由 Krant5 于 2016-8-14 10:53 编辑
liyuanjun 发表于 2016-8-14 09:55
请问你用的这个软件叫什么??


DEV-C 其实跟软件没关系吧,我用codeblock一样能编出来,你说你到底怎么操作的哦
50A3.tmp.jpg

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
liyuanjun + 5 + 5 + 3 热爱鱼C^_^

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-14 14:59:49 | 显示全部楼层
Krant5 发表于 2016-8-14 10:48
DEV-C 其实跟软件没关系吧,我用codeblock一样能编出来,你说你到底怎么操作的哦

我就是这样操作的,我用别的软件运行正常,可能是我软件的原因吧,还是很谢谢你!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-15 13:45:36 | 显示全部楼层
你在代码最后面加上
void print(struct student* head)
{
        printf("num=%d,score=%f\n",head.num,head.score);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-18 14:52:41 | 显示全部楼层
可以先将报错的英文翻译成中文,然后再解决问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-23 20:27:41 | 显示全部楼层
厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-2 09:01:17 | 显示全部楼层
在student.h问下的最上面加一句#pragma once 试一试,我估计是编译查到你的文件里面包含了多个一样的头文件,重复包含可能会使编译

器跳过你的student.cpp的文件。

还有建议你把所有的头文件声明都放在.h文件里,类实现文件只需要包含以下.h文件就OK了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-9-2 21:33:45 | 显示全部楼层
头文件少了#include<upstream>
另外,主程序中定义类对象时,不需要再加class.
程序看了 没问题啊。再编译试试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-11-21 21:29:56 | 显示全部楼层
你类中的函数定义在哪????????
这个错误提示,通常是只有函数名, 但没有函数定义
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-12-14 20:16:07 | 显示全部楼层
没有问题呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-12-14 20:16:40 | 显示全部楼层
你怎么搞的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 13:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表