【问题标题】:How do I successfully implement my .h file to my main .cpp file to make it run without errors, examp,es would be very helpful thank you [closed]我如何成功地将我的 .h 文件实施到我的主 .cpp 文件中以使其正常运行,例如,es 将非常有帮助,谢谢[关闭]
【发布时间】:2021-12-25 21:36:46
【问题描述】:

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

每当我运行代码时,它都会读取以下错误:

program3.cpp:5:21: error: cannot call constructor ‘dog_list::dog_list’ directly                                                                                                                                                              [-fpermissive]
    5 |  dog_list::dog_list() = default;
      |                     ^
program3.cpp:5:21: note: for a function-style cast, remove the redundant ‘::dog_                                                                                                                                                             list’
program3.cpp:5:25: error: expected primary-expression before ‘default’
    5 |  dog_list::dog_list() = default;
      |                         ^~~~~~~
program3.cpp:6:22: error: no matching function for call to ‘dog_list::~dog_list(                                                                                                                                                             )’
    6 |  dog_list::~dog_list() = default;
      |                      ^
program3.h:12:2: note: candidate: ‘dog_list::~dog_list()’
   12 |  ~dog_list();
      |  ^
program3.h:12:2: note:   candidate expects 1 argument, 0 provided
program3.cpp:6:26: error: expected primary-expression before ‘default’
    6 |  dog_list::~dog_list() = default;
      |                          ^~~~~~~

我的 .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];
};
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;    
    }   
}

【问题讨论】:

  • 嗨冬青,我只是好奇你在哪里添加的
  • (有点)类似的问题:stackoverflow.com/questions/69960187/…
  • 没关系,我测试的代码不正确。 :/ 至少还有一个 .cpp 文件您没有向我们展示。

标签: c++ class constructor


【解决方案1】:

你声明了一个构造函数和析构函数,但是你没有像 record_pets 成员函数那样实现这些函数。另外,你忘了在 main 中声明 op 是什么类型

您不需要析构函数 - 因为没有任何东西需要清理而没有为您处理。但您可能确实需要一个构造函数实现。

您将数组大小声明为SIZE=20,但您在cin 语句中读取了最多25 个字符。所以让我们解决这个问题。而不是这样的陈述:

cin.get(op.service,25,'\n');

这样说:

cin.get(op.service,dog_list::SIZE,'\n');

我下面的其他内容主要只是代码清理。

program3.h如下:

#pragma once

class dog_list
{
public:
    static const int SIZE = 25;
    dog_list();
    void record_pets();
private:
    char name[SIZE];
    char breed[SIZE];
    char species[SIZE];
    char service[SIZE];
    char special[SIZE];
};

main.cpp如下:

#include<iostream>
#include<cstring>
#include<cctype>
#include<fstream>
#include "program3.h"

using namespace std;
const int END = 11;


dog_list::dog_list() :
    name(),  // zero-init each array member
    breed(),
    species(),
    service(),
    special()
{
}


void dog_list::record_pets()
{

    // all that code you have in your implementation above
}

int main()
{
    dog_list op;
    op.record_pets();
    return 0;
}

【讨论】:

  • 你好 selbie,谢谢你这么好的回答,我现在遇到的问题是我做了所有的改变,但是我现在得到这个错误:program3.cpp: In member function 'void dog_list:: record_pets()': program3.cpp:23:11: error: 'op' 未在此范围内声明 23 | cin.get(op.name,dog_list::SIZE,'\n'); | ^~
  • 代替cin.get(op.service,dog_list::SIZE,'\n');,说cin.get(service,dog_list::SIZE,'\n'); 对所有其他违规行重复。
  • 谢谢,我想我几乎可以正常工作了,我现在收到错误 program3test.cpp: In function 'int main()': program3test.cpp:23:2: error: 'record_pets'未在此范围内声明 23 |记录宠物(); | ^~~~~~~~~~~~~~
  • 我还对您建议的以下代码的用途感到困惑: dog_list::dog_list(): name(), // 对每个数组成员进行零初始化breed(), species(),服务(),特殊()
  • 这称为构造函数初始化列表。通过指定 breed() 和类似的声明,它会将你的数组变量初始化为零,因此它里面没有垃圾。
猜你喜欢
  • 2021-12-25
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 2018-10-11
  • 2016-06-18
  • 2022-12-04
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多