澄清:
此项目在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);
};
print方法的前两个实例工作正常,但第三个(带有字符串/ FText作为参数的实例)给出了以下错误:
"no instance of overloaded function "CowsAndBulls::Display::print" matches the specified type"
为了增加一些额外的清晰度,代码仍然编译,我还没有使用过这个函数:我在这个版本的print的定义中遇到错误。
我很确定我只是错过了一些简单的东西,但快速搜索堆栈溢出的问题却未能给我带来另一个与此情况完全相同的问题(或许我只是没有认识到另一个帖子是同样的问题? )
无论哪种方式,任何援助表示赞赏。
好吧,看起来我忘了在View类头文件中包含stdafx.h。虽然stdafx.h(以及扩展名,FText)包含在cpp文件中,但是头文件显然不知道它是什么。这让我失望,因为错误显示在cpp文件中。
为了将来参考,有谁知道为什么会这样?