c++ 尝试将函数从头文件转换为cpp文件时会出现无尽的错误

tjrkku2a  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(95)
#ifndef LibraryUser_h
#define LibraryUser_h

#include <string>
using namespace std;

class LibraryUser {
private:
    std::string name;
    std::string email;
    int idnumber;
public:
    LibraryUser(string name, string email, int idnumber);
    LibraryUser();
    void addnewuser();
    string getName();
    string getEmail();
    int getIdnumber();
    void setName(string name);
    void setEmail(string email);
    void setIdnumber(int idnumber);
};

#endif

字符串
美共

#include "LibraryUser.h"
#include <string>
#include <iostream>
using namespace std;

LibraryUser(string name, string email, int idnumber) {
    this->name = name;
    this->email = email;
    this->idnumber = idnumber;
};
void setName(string name) {
    name = name;
}
void setEmail(string email) {
    email = email;
}
void setIdnumber(int idnumber) {
    idnumber = idnumber;
}

void addnewuser() {

    string objectname;
    cout << "Enter name of object.";
    cin >> objectname;
    cout << "Enter the name of the user you would like to add:";
    cin >> name;
    cout << "Enter the email for the user.";
    cin >> email;
    cout << "Enter the idnumber for the user: ";
    cin >> idnumber;
    LibraryUser objectname(name, email, idnumber);

}


我只是得到了一堆错误,我正在努力修复它,即使在人工智能的帮助下。
我尝试使用用户输入来创建一个对象,但由于这是我第一次使用头文件,我遇到了一些问题。我得到的错误是cpp文件中第五个name之前的')',然后makenewuser函数中使用的所有变量都没有得到未声明的错误,即使构造函数将它们全部删除。

hgtggwj0

hgtggwj01#

在你的.cpp文件中,你需要提供你正在定义的函数的全限定名。例如,你的构造函数定义需要是:

LibraryUser::LibraryUser(string name, string email, int idnumber) {
    this->name = name;
    this->email = email;
    this->idnumber = idnumber;
}

字符串
其他成员函数也是如此,如

void LibraryUser::setName(string name) {
    name = name;
}


相关问题