C++ Visual Studio 重载函数错误:没有重载函数的实例与指定的类型匹配

C++ Visual Studio overloaded function error: no instance of overloaded function matches the specified type

本文关键字:重载 函数 实例 类型 Studio Visual 错误 C++      更新时间:2023-10-16

澄清:

这个项目是在Visual Studio中完成的,使用了一个stdafx.h文件。

FText 是字符串的别名,如文件 stdafx.h 中建立的那样:

#include <string>
using FText = std::string;
using int32 = int;

stdafx.h 包含在 View.cpp 类中。

原始问题:

因此,我正在尝试创建几种打印方法,这些方法基于一个或两个参数打印字符串:指示要打印的消息的数字参数,以及要在消息中使用的输入。截至目前,我有三个"打印"方法的实例,如下所示:

///A method that prints a message
//give it a number to tell it what message to print.
void Display::print(int message)
{
switch (message)
{
case 0:
std::cout << "Welcome to Cows and Bulls, a simple word-guessing game. n";
break;
case 1:
//(I omitted most of the messages to save space)
break;
default:
std::cout << "Something has gone horribly wrong. n";
std::cout << "Goodbye. n";
throw std::exception();
}

}
//messages with one int parameter
void Display::print(int message, int param)
{
switch (message)
{
case 0:
break;
default:
std::cout << "Something has gone horribly wrong. n";
std::cout << "Goodbye. n";
throw std::exception();
}
}
//messages with one string parameter
void Display::print(int message, FText param)
{
switch (message)
{
case 0:
break;
default:
std::cout << "Something has gone horribly wrong. n";
std::cout << "Goodbye. n";
throw std::exception();
}
}

我还没有向第二个和第三个重载添加任何消息,但我会解决这个问题。

程序视图部分的头文件中的类声明如下所示:

#pragma once
//The method that prints thing to the screen

class Display
{
public:
void print(int message);
//print a message with a parameter other than the message being selected
void print(int message, int param);
void print(int message, FText param);
};

打印方法的前两个实例工作正常,但第三个实例(以字符串/FText 作为参数的实例(给我以下错误:

"no instance of overloaded function "CowsAndBulls::Display::print" matches the specified type"

为了增加一点额外的清晰度,代码仍在编译,我还没有使用这个函数:我在这个版本的打印的定义中遇到错误。

我相当确定我只是错过了一些简单的东西,但是快速搜索堆栈溢出的问题未能给我带来另一个与此情况完全相同的问题(或者也许我只是没有将另一篇文章识别为同一问题?

无论哪种方式,任何帮助都值得赞赏。

好的,看起来我忘了在查看类头文件中包含 stdafx.h。虽然 stdafx.h(以及扩展的 FText(包含在 cpp 文件中,但头文件显然不知道它是什么。这让我很失望,因为错误显示在 cpp 文件中。

供将来参考,有谁知道为什么会这样?