C++ 字符串函数定义的语法
有人能解释一下我在下面的语法中犯了什么错误吗?我的教科书没有任何用户定义字符串函数的例子,我也不知道我应该做什么。提前谢谢C++ 字符串函数定义的语法,c++,visual-studio,C++,Visual Studio,有人能解释一下我在下面的语法中犯了什么错误吗?我的教科书没有任何用户定义字符串函数的例子,我也不知道我应该做什么。提前谢谢 #include <iostream> #include <cmath> #include <cstdlib> #include <string> string dayOfWeek(int day); using namespace std; int main() { cout << "Ple
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
string dayOfWeek(int day);
using namespace std;
int main()
{
cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
cin >> day;
cout << "The name of the day of the week is: " dayOfWeek(day) << endl;
}
string dayOfWeek(int day)
{
if (day == 0)
return "SUNDAY";
else if (day == 1)
return "MONDAY";
else if (day == 2)
return "TUESDAY";
else if (day == 3)
return "WEDNESDAY";
else if (day == 4)
return "THURSDAY";
else if (day == 5)
return "FRIDAY";
else if (day == 6)
return "SATURDAY";
}
应按预期工作。问题的关键在于:
string dayOfWeek(int day);
using namespace std;
使用是在依赖于它的代码之后,因此编译器查找字符串,但找不到它。它不会去寻找std::string,因为它还没有被告知要这样做
你可以用几行字向上移动,但由于答案中包含的许多原因和一些未包含的原因,你最好不要推迟
在你的代码中。显式地在std::前面加前缀,可以避免一连串令人讨厌的意外
我还建议遵循@πάνταῥεῖ 函数放置位置的示例。前向声明和后向声明为您提供了两个必须更改代码的位置和一个出现错误的位置。提前声明将要使用的函数意味着您只需更改一个声明
将其与其他一些小调整结合起来:
#include <string>
#include <iostream>
std::string dayOfWeek(int day)
{
switch (day) // for stuff like this, I like a switch. I find it looks nicer.
{
case 0:
return "SUNDAY";
case 1:
return "MONDAY";
case 2:
return "TUESDAY";
case 3:
return "WEDNESDAY";
case 4:
return "THURSDAY";
case 5:
return "FRIDAY";
case 6:
return "SATURDAY";
default:
return "No such day"; // functions must always return something.
// in this case what will cout print if you
// don't return a string? Don't know?
// Don't feel bad. Neither do I.
// Welcome to Undefined Behaviour
}
}
int main()
{
int day; //this was missing
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
std::cin >> day;
std::cout << "The name of the day of the week is: " << dayOfWeek(day) << std::endl;
}
您的代码不完整,至少缺少一些行。请提供您面临的错误/问题。您需要发布一个最小、完整且可验证的示例。这甚至没有编译…投票结束,因为缺少一个可复制的例子。请提供一个完整但最小的示例,读者可以复制并粘贴以尝试。最小示例现已上载。很抱歉,这个网站是新来的,不知道这是预期的。其他问题包括没有包含编译代码的示例:没有看到。错过这一天会毁了你整个星期。我犯了个愚蠢的错误。我只是从我的大程序中复制代码,不小心把名称空间放错了位置。我听说过使用名称空间std可能导致的问题,但我正在上一堂大学课,我的教授说要使用它,所以我想我现在最好还是坚持使用它。@BrianW Rebel!不要跟随机构。下次还要阅读编译器错误,当然要在以后的所有问题中包含它们。
using namespace std;
#include <string>
#include <iostream>
std::string dayOfWeek(int day)
{
switch (day) // for stuff like this, I like a switch. I find it looks nicer.
{
case 0:
return "SUNDAY";
case 1:
return "MONDAY";
case 2:
return "TUESDAY";
case 3:
return "WEDNESDAY";
case 4:
return "THURSDAY";
case 5:
return "FRIDAY";
case 6:
return "SATURDAY";
default:
return "No such day"; // functions must always return something.
// in this case what will cout print if you
// don't return a string? Don't know?
// Don't feel bad. Neither do I.
// Welcome to Undefined Behaviour
}
}
int main()
{
int day; //this was missing
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
std::cin >> day;
std::cout << "The name of the day of the week is: " << dayOfWeek(day) << std::endl;
}
#include <string>
#include <iostream>
// define an array of days of the week to print. const means dayOfWeek cannot be changed
const std::string dayOfWeek[] =
{
"SUNDAY",
"MONDAY",
"TUESDAY",
"WEDNESDAY",
"THURSDAY",
"FRIDAY",
"SATURDAY"
};
int main()
{
unsigned int day;
//Note unsigned int. Negative numbers are now disallowed
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
// keep looping until user provides a good number
while (!(std::cin >> day) // user MUST input a number
|| day > 6) // number is a usable number
{
std::cin.clear(); // clean up bad input
std::cout << "Please enter a day of week (0 for Sunday, 1 for Monday, etc): ";
}
// since we know day is a usable (0..6) number we can read the day out of the array
std::cout << "The name of the day of the week is: " << dayOfWeek[day] << std::endl;
}