VS为我提供了对构造函数的另一个解决方案,但我想知道为什么我的工作不起作用

VS is giving me another solution to my constructor but I want to know why mine wont work

本文关键字:想知道 解决方案 为什么 不起作用 我的工作 另一个 构造函数 VS      更新时间:2023-10-16

我的构造函数下面有一个绿线,说"未找到功能定义"。

Visual Studio为我提供了解决方案,但我想知道为什么我的工作不起作用。

#pragma once
#include "class_dayType.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
    dayType day;
    string d;
    cout << "Enter day of week: ";
    cin >> d;
    day.set_day(d);
}

#include <iostream>
#include<string>
using namespace std;
class dayType {
public:
    string day;
    dayType();  //constructor with green line
    void set_day(string day_of_week) {
        string day = day_of_week;
    }
};

Visual Studio在另一个文件中创建了此功能,并且起作用。这与我的构造函数有什么区别?

dayType::dayType()
{
}

错误:

lnk2019未解决的外部符号" public:__thiscall daytype :: daytype(void("(?? 0dayType @@ qae@qae@xz(在函数中引用_main day_of_week

lnk1120 1未解决的外部day_of_week

dayType(); 

这不是定义,而只是声明。它表明构造函数(或任何函数(将在代码中的某个地方存在。

您需要

dayType() 
{
}

在这里和这里阅读更多。