未定义的标识符和未解决的外部错误 C++ Visual Studio 2022

Undefined identifier and unresolved external errors C++ Visual Studio 2022

这基本上是两个错误合二为一,似乎来自同一件事。如果我在我的 main.cpp 文件中定义我的函数,并在文件顶部转发声明它们,它不会给我任何错误并且一切运行顺利。

当尝试整理并将内容移动到单独的 .cpp 文件中时,它开始给我链接器错误并说内容未定义,即使我使用的是适当的头文件。代码如下:

main.cpp

#include "io.h"

#include <iostream>
#include <map>


class exchangeRates;
void menu();

int main()
{
    menu();
    return EXIT_SUCCESS;
}

// class storing exchange rates

class exchangeRates
{
public: // Access specifier

    // Data Members
    std::map<std::string, double> usd = {
                                {"GBP", 1.2},
                                {"EUR", 0.7},
    };
    std::map<std::string, double> gbp = {
                                {"USD", 0.9},
                                {"EUR", 1.4},
    };
};

// menu function

void menu()
{
    // get reference currency code from user
    std::string refCurrency{ obtainCodeFromUser() };

    // create 'rates' instance of 'exchangeRates' class
    exchangeRates rates{};

    // print the exchange values for that currency
    if (refCurrency == "USD")
    {
        printExchangeValues(rates.usd, refCurrency);
    }
    else if (refCurrency == "GBP")
    {
        printExchangeValues(rates.gbp, refCurrency);
    }
    else
    {
        std::cout << "\nInvalid currency code. Example: USD, GBP, EUR etc.\n\n";
        menu();
    }
}

io.h

#ifndef IO_H
#define IO_H

#include <iostream>
#include <map>

std::string obtainCodeFromUser();
double obtainAmountFromUser();
template<typename Map>
void printExchangeValues(Map& valuedCurrencies, std::string refCurrency);

#endif

io.cpp

#include "io.h"

#include <iostream>
#include <map>

// io functions for currency converter

std::string obtainCodeFromUser()
{
    // obatin reference currency code from user
    std::cout << "Enter currency code for reference currency (case-sensitive): ";
    std::cin.clear();
    std::string refCurrency{};
    std::cin >> refCurrency;
    std::cin.ignore(INT_MAX, '\n');

    return refCurrency;
}

double obtainAmountFromUser()
{
    // obtain amount of currency to be converted from user
    std::cout << "Enter amount of currency to convert: ";
    std::cin.clear();
    double amount{};
    std::cin >> amount;
    std::cin.ignore(INT_MAX, '\n');

    return amount;
}

template<typename Map>
void printExchangeValues(Map& valuedCurrencies, std::string refCurrency)
{
    // obtain amount of currency to be converted
    double amount{ obtainAmountFromUser() };
    std::cout << refCurrency << " " << amount << " is worth:\n";
    for (auto& item : valuedCurrencies) {
        std::cout << item.first << ": " << amount * item.second << '\n';
    }
}

我仍然是 C++ 的初学者,所以有些部分是从其他开源程序中复制的,我知道我的代码可能有很多地方可以改进,但我主要感兴趣的是它为什么会赢'编译。我在学习如何使用头文件时遵循的示例运行良好,我认为我在这里没有做任何不同的事情。

它说标识符“obtainCodeFromUser”和“printExchangeValues”未定义。 它给出的链接器错误是 LNK2019 'unresolved external symbol ...',它似乎与 printExchangeValues 函数有关。

非常感谢任何帮助!

WhozCraig提到的issue很有用,希望大家仔细阅读。关于你的问题,我修改了一些代码后,程序可以运行正确。错误是由模板引起的。由于你是初学者,程序也不是很复杂,下面的代码更方便你理解:
main.cpp

#include "io.h"

#include <iostream>
#include <map>

// class storing exchange rates

class exchangeRates
{
public: // Access specifier

    // Data Members
    std::map<std::string, double> usd = {
                                {"GBP", 1.2},
                                {"EUR", 0.7},
    };
    std::map<std::string, double> gbp = {
                                {"USD", 0.9},
                                {"EUR", 1.4},
    };
};
template<typename Map>
void printExchangeValues(Map& valuedCurrencies, std::string refCurrency)
{
    // obtain amount of currency to be converted
    double amount{ obtainAmountFromUser() };
    std::cout << refCurrency << " " << amount << " is worth:\n";
    for (auto& item : valuedCurrencies) {
        std::cout << item.first << ": " << amount * item.second << '\n';
    }
}
// menu function

void menu()
{
    // get reference currency code from user
    std::string refCurrency{ obtainCodeFromUser() };

    // create 'rates' instance of 'exchangeRates' class
    exchangeRates rates{};

    // print the exchange values for that currency
    if (refCurrency == "USD")
    {
        printExchangeValues(rates.usd, refCurrency);
    }
    else if (refCurrency == "GBP")
    {
        printExchangeValues(rates.gbp, refCurrency);
    }
    else
    {
        std::cout << "\nInvalid currency code. Example: USD, GBP, EUR etc.\n\n";
        menu();
    }
}

int main()
{
    menu();
    return EXIT_SUCCESS;
}

io.h

#ifndef IO_H
#define IO_H

#include <iostream>
#include <map>

std::string obtainCodeFromUser();
double obtainAmountFromUser();


#endif

io.cpp

#include "io.h"

#include <iostream>
#include <map>

// io functions for currency converter

std::string obtainCodeFromUser()
{
    // obatin reference currency code from user
    std::cout << "Enter currency code for reference currency (case-sensitive): ";
    std::cin.clear();
    std::string refCurrency{};
    std::cin >> refCurrency;
    std::cin.ignore(INT_MAX, '\n');

    return refCurrency;
}

double obtainAmountFromUser()
{
    // obtain amount of currency to be converted from user
    std::cout << "Enter amount of currency to convert: ";
    std::cin.clear();
    double amount{};
    std::cin >> amount;
    std::cin.ignore(INT_MAX, '\n');

    return amount;
}