C++ C++;我的函数出现错误
需要一些家庭作业帮助。我是C++新手,我有一个我不理解的错误。这是我的密码:C++ C++;我的函数出现错误,c++,function,scope,declare,C++,Function,Scope,Declare,需要一些家庭作业帮助。我是C++新手,我有一个我不理解的错误。这是我的密码: /* * homework6.cpp * Coder: omega9380 * Final Project */ #include <iostream> #include <string> #include <iomanip> using namespace std; int main () { void welcomeScreen(); void pr
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main () {
void welcomeScreen();
void printLine( int length );
welcomeScreen();
return 0;
}
void welcomeScreen() {
string userName = "";
string title1 = "CMPSC101 FINAL PROJECT";
string title2 = "CREATED BY: OMEGA9380";
// Welcome screen:
system("CLS");
cout << "/";
printLine(80);
cout << "\\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (title1.length() / 2)) << title1 << setw(40 - (title1.length() / 2)) << "|" << endl;
cout << "|" << setw(41 + (title2.length() / 2)) << title2 << setw(40 - (title2.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\\";
printLine(80);
cout << "/" << endl;
}
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
/*
*家庭作业6.cpp
*编码员:omega9380
*最终项目
*/
#包括
#包括
#包括
使用名称空间std;
int main(){
void welcomeScreen();
无效打印线(整数长度);
欢迎光临屏幕();
返回0;
}
void welcomeScreen(){
字符串userName=“”;
字符串title1=“CMPSC101最终项目”;
字符串title2=“创建人:OMEGA9380”;
//欢迎屏幕:
系统(“CLS”);
您的函数没有声明,所以请这样使用:
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void welcomeScreen() {
string userName = "";
string title1 = "CMPSC101 FINAL PROJECT";
string title2 = "CREATED BY: OMEGA9380";
// Welcome screen:
system("CLS");
cout << "/";
printLine(80);
cout << "\\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (title1.length() / 2)) << title1 << setw(40 - (title1.length() / 2)) << "|" << endl;
cout << "|" << setw(41 + (title2.length() / 2)) << title2 << setw(40 - (title2.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\\";
printLine(80);
cout << "/" << endl;
}
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
int main () {
void welcomeScreen();
void printLine( int length );
welcomeScreen();
return 0;
}
/*
*家庭作业6.cpp
*编码员:omega9380
*最终项目
*/
#包括
#包括
#包括
使用名称空间std;
void welcomeScreen(){
字符串userName=“”;
字符串title1=“CMPSC101最终项目”;
字符串title2=“创建人:OMEGA9380”;
//欢迎屏幕:
系统(“CLS”);
cout您正在main()
函数的范围内声明printLine()
函数。此声明在welcomeScreen()的定义中不可见
将printLine
的声明移到main
之外和welcomeScreen
对于welcomeScreen
的声明也应该这样做,假设您真的不想在main中声明函数,您需要转发声明welcomeScreen和打印行函数:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int printLine(int);
void welcomeScreen();
int main () {
welcomeScreen();
return 0;
}
void welcomeScreen() {
// definition
}
void printLine(int length) {
// definition
}
#包括
#包括
#包括
使用名称空间std;
int打印行(int);
void welcomeScreen();
int main(){
欢迎光临屏幕();
返回0;
}
void welcomeScreen(){
//定义
}
无效打印行(整数长度){
//定义
}
或者在main之前定义它们:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int printLine(int length) {
//definition
}
void welcomeScreen() {
// definition
}
int main () {
welcomeScreen();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
整数打印行(整数长度){
//定义
}
void welcomeScreen(){
//定义
}
int main(){
欢迎光临屏幕();
返回0;
}
您的printLine函数仅在main中声明,因此welcomeScreen函数无法看到它
您应该将welcomeScreen和printLine函数移到main之前,并确保printLine位于welcomeScreen之前,以便welcomeScreen在尝试调用它之前知道它存在
像这样:
/*
* homework6.cpp
* Coder: omega9380
* Final Project
*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void printLine( int length ) {
for ( int i = 0; i < length; i++ ) {
cout << "=";
}
}
void welcomeScreen(void) {
string userName = "";
string title1 = "CMPSC101 FINAL PROJECT";
string title2 = "CREATED BY: OMEGA9380";
// Welcome screen:
system("CLS");
cout << "/";
printLine(80);
cout << "\\" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "|" << setw(41 + (title1.length() / 2)) << title1 << setw(40 - (title1.length() / 2)) << "|" << endl;
cout << "|" << setw(41 + (title2.length() / 2)) << title2 << setw(40 - (title2.length() / 2)) << "|" << endl;
cout << "|" << setw(81) << "|" << endl;
cout << "\\";
printLine(80);
cout << "/" << endl;
}
int main () {
welcomeScreen();
return 0;
}
/*
*家庭作业6.cpp
*编码员:omega9380
*最终项目
*/
#包括
#包括
#包括
使用名称空间std;
无效打印行(整数长度){
for(int i=0;i 无法添加前向声明或向上移动函数。在任何其他函数之外更容易声明函数。关键是“在此范围内”void printLine(int-length);
是在main
中声明的,所以它只在main
的范围内可见。你的答案是正确的。它只是有改进的余地。我不知道为什么它会被否决。我也不知道,但你能做什么:)我知道回答这个问题的人都会被否决这里有一个向上的答案,你已经在努力回答一个问题d被投票否决了lul。我猜它不能满足OP或其他要求。帮助某人获得-1,你们这些人真是难以置信。这解决了它。我不知道你们可以在main()之外声明变量和函数。谢谢!!@JeremyKellogg当然可以!欢迎你们的朋友,如果你们有任何问题,请随时与我联系,我很乐意提供帮助。