RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 584426
Accepted
E1mir
E1mir
Asked:2020-10-30 06:07:54 +0000 UTC2020-10-30 06:07:54 +0000 UTC 2020-10-30 06:07:54 +0000 UTC

编译Dev-c++时出现Undefined reference错误

  • 772

下午好!我在编译的时候用C++写了一个简单的类,我遇到了这样一个奇怪的问题..我对C++不太了解,但这是我的.h和.cpp文件:

如果代码的拼写有错误,请留下这样的注释以备后用:C++不能这样写,但你要喜欢这样……

学生.cpp

#include "Student.h"
#include <string>
using std::string;

Student::Student(){}

void Student::setName(string n){
    name = n;
}
void Student::setSurname(string s){
    surname = s;
}
void Student::setAge(int a){
    age = a;
}
void Student::setCourse(int c) {
    course = c;
}
void Student::setGroup(string g){
    group = g;
}
void Student::setMarks(string* mark){
    marks = mark;
}
string Student::getName(){
    return name;
}
string Student::getSurname(){
    return surname;
}
int Student::getAge(){
    return age;
}
int Student::getCourse(){
    return course;
}
string Student::getGroup(){
    return group;
}
string* Student::getMarks(){
    return marks;
} 

学生.h

#ifndef Student_h
#define Student_h
#include <string>
using std::string;
class Student{

    public:
        Student();
        void setName(string);
        string getName();
        void setSurname(string);
        string getSurname();
        void setAge(int);
        int getAge();
        void setCourse(int);
        int getCourse();
        void setGroup(string);
        string getGroup();
        void setMarks(string*);
        string* getMarks();

    private:
        string name;
        string surname;
        int age;
        int course;
        string group;
        string* marks;
};
#endif

主.cpp

#include <stdio.h>
#include <iostream>
#include "Student.h"
using std::cout;
using std::endl;
int main(){
    Student test;
    string marks[] = {"B","B","C","A","A","A"};
    test.setName("Name");
    test.setSurname("Surname");
    test.setAge(100);
    test.setCourse(100);
    test.setGroup("606.4");
    test.setMarks(marks);

    cout << test.getName() << endl;
    cout << test.getSurname() << endl;
    cout << test.getAge() << endl;
    cout << test.getCourse() << endl;
    cout << test.getGroup() << endl;
    string* mark = test.getMarks();
    for(int i = 0 ; i < 6 ; i++){
        cout << mark[i] <<" ";
    }
}

我认为我做的一切都是对的,但是在编译过程中出现了一个奇怪的错误......

这是编译的时候Main.cpp

这是在编译 Main 方法时

这是编译时Student.cpp

编译学生

告诉我我所有的错误在哪里?

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Harry
    2020-10-30T13:54:36Z2020-10-30T13:54:36Z

    至于为什么它不编译 - 正如已经提到的,你的错误是链接错误,所以这个文件没有连接到项目Student.cpp,而找不到的WinMain那个表明你正在尝试创建一个窗口应用程序,而不是控制台应用程序。

    但是你还有问题的第二部分——如何写。你没有错误——从某种意义上说它们会导致程序失败。但是,大致就像你制定的那样 - 你不应该像那样用 C ++ 编写......

    我首先要说的是,您实际上没有课程,而是结构。从某种意义上说,您的类不包含任何应保持一致状态的特定内部状态,或除设置和返回字段之外的任何其他功能。因此,我个人认为简单地将您的类声明为一个结构(即具有所有公共成员的类)并直接与它们一起工作并不是什么大罪:

    struct Student{
        string name;
        string surname;
        int age;
        int course;
        string group;
        string* marks;
    };
    ...
    Student test;
    test.name = "Name";
    

    如果计划进行一些更严肃的工作,或者作业要求它是一个类,那么最好将这种小的单行函数内联(嵌入),即 直接在类声明中定义它们。这将允许编译器不调用它们,而只是将它们内联到代码中,这对性能有好处。还有一件事——我会将所有返回函数设为 const——这样它们也可以在 const 对象上调用。我会将行作为常量引用进行传输,以减少复制。

    class Student{
      public:
        Student();
        void   setName(const string& s)    { name = s; }
        string getName() const             { return name; }
        void   setSurname(const string& s) { surname = s; }
        string getSurname() const          { return surname; }
    
        ...
      private:
        string name;
        string surname;
        int age;
        int course;
        string group;
        string* marks;
    };
    

    另外,你的分数显然是单字符的,所以我不会使用字符串数组,而只是使用一个字符串——一般来说,它是一个字符数组(同时,我会添加一个函数来添加一个分数- 即连接下一个字符的行)。此外,您将摆脱所谓的“魔法常量”——在推导有 6 个评级时您不必记住:

    for(char c : mark){
        cout << c <<" ";
    }
    

    然而 - 你将所有行复制到 class中,这是正确的 - 对象结果是自给自足的,“我随身携带一切。” 但是您只需将此数组作为指向类外部实体的指针传递。如果它发生变化,那么类的状态也会发生变化。粗略地说,你的学生的记录簿不是记录簿,而是一张纸的封面,上面写着——“我的成绩写在那里,在黑板上”——任何欺负者都可以纠正它们……即 即使您使用的是字符串数组,也最好将其复制到内部,以实现更好的封装。

    然而——你为什么不创建另一个构造函数来立即创建一个命名的学生?名字和姓氏不太可能改变(除非学生结婚 :))。

    • 4

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5