【问题标题】:How do I successfully implement my .h file to my main .cpp file to make it run without errors如何成功地将我的 .h 文件实施到我的主 .cpp 文件中以使其正常运行
【发布时间】:2021-12-25 19:42:42
【问题描述】:

代码应该从 .h 文件中获取类,并在主文件中使用它来创建自定义宠物概要,稍后可以将其存储在另一个文本文件中。我还没有对文本文件进行模块化提取,因为我至少需要让它工作并且能够实际编译和返回构成自定义宠物概要的不同数组。

/usr/bin/ld: /tmp/: in function `__static_initialization_and_destruction_0(int, int)':
program3.cpp:(.text+0x411): undefined reference to `dog_list::dog_list()'
/usr/bin/ld: program3.cpp:(.text+0x426): undefined reference to `dog_list::~dog_list()'
collect2: error: ld returned 1 exit status

我的 .h 文件

#include<iostream>
#include<cstring>
#include<cctype>
#include<fstream>
using namespace std;
const int SIZE = 20;
const int END = 11;
class dog_list
{
    public:
    dog_list();
    ~dog_list();
    void record_pets();
    private:
    char name[SIZE];
    char breed[SIZE];
    char species[SIZE];
    char service[SIZE];
    char special[SIZE];
};
dog_list op;


void record_pets();

我的主 .cpp 文件

#include "program3.h"
int main()
{
       op.record_pets();
        return 0;

}


void dog_list::record_pets()
{
    char personal_list[SIZE];
    int i = 0;
    char again;

    do
    {

        cout << "Enter in pets name: ";
        cin.get(op.name,25,'\n');
        cin.ignore(100,'\n');   
        cout << endl << "Enter breed of pet:  ";
        cin.get(op.breed, 25, '\n');
        cin.ignore(100,'\n');
        cout << endl << "Enter species: ";
        cin.get(op.species,25,'\n');
        cin.ignore(100,'\n');
        cout << endl <<  "Enter in service qualifications: ";
        cin.get(op.service,25,'\n');
        cin.ignore(100,'\n');
        cout << endl << "Enter in special notes: ";
        cin.get(op.special,25,'\n');
        cin.ignore(100,'\n');
        cout << endl;

        cout << "Name:  " << op.name << endl;
        cout <<"Breed: " <<  op.breed << endl;
        cout << "Species: " << op.species << endl;
        cout << "Service Qualifications: " << op.service << endl;
        cout << "Special Notes: " << op.special << endl;

        cout << "Pet saved! Would you like to enter another pet? Y/N: " << endl;
        cin >> again;
        cin.ignore(100,'\n');
        cout << endl;

        if(again == 'y')
        {
            again = toupper(again);
        }


    }while(again == 'Y' && i <= END);
    {
        ++i;    
    }   
}

【问题讨论】:

  • 您的头文件声明 dog_list() 的构造函数,但没有定义(即实现它)。您收到的消息来自链接器,并报告它找不到该构造函数的定义。因此,您需要实现构造函数。您的代码中还有其他问题,但这是您询问的问题。

标签: c++ class


【解决方案1】:

你永远不会实现构造函数和析构函数,你只是声明它:

dog_list();
~dog_list();

你必须实现它,例如,在 main.cpp 中:

dog_list::dog_list() = default;
dog_list::~dog_list() = default;

【讨论】:

  • 非常感谢您,我按照您所说的做了,现在它给了我常规的错误,但是现在我收到了这些错误,我不知道如何修复它们。 program3.cpp:4:21: 错误: 不能直接调用构造函数‘dog_list::dog_list’ [-fpermissive] 4 |dog_list::dog_list() = default; program3.cpp:4:21: 注意:对于函数式转换,删除多余的 '::dog_list' program3.cpp:4:25: error: expected primary-expression before 'default' 4 |dog_list::dog_list( ) = 默认值; program3.cpp:5:22: error: no matching function for call to ‘dog_list::~dog_list()’ 5 |dog_list::~dog_list() = default;
  • 您可能正在使用 C++11 之前的编译器。在 C++11 之前,=default 构造不是标准 C++ 的一部分。
猜你喜欢
  • 2021-12-25
  • 2021-03-18
  • 2021-12-28
  • 2012-08-18
  • 2011-05-23
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多