2017-04-10 27 views
-2

需要一些家庭作業幫助。我是C++的新手,並且遇到了一個我不明白的錯誤。這裏是我的代碼:C++通過我的功能獲取錯誤

/* 
* 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 << "="; 
    } 
} 

錯誤是「error:'printLine'未在此範圍內聲明」。我在main()函數中聲明瞭「printLine()」,這還不夠嗎?還是我需要在每一個我打算使用它的函數中聲明函數名?爲了回答這個棘手的問題,我必須在這個最終的項目中使用函數。謝謝!!!

+2

添加前置聲明或移動功能了。 –

+0

更容易在任何其他功能之外聲明您的功能。 – aschepler

+1

關鍵是「在此範圍內」void printLine(int length);'在'main'內聲明,所以它只能在'main'範圍內可見。 – user4581301

回答

3

聲明做假設你真的不希望在主已經宣佈功能,你要麼需要向前聲明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 
} 

或之前主只是定義它們:

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

int printLine(int length) { 
    //definition 
} 

void welcomeScreen() { 
    // definition 
} 

int main() { 
    welcomeScreen(); 
    return 0; 
} 
+0

修復它。我沒有意識到你可以在main()之外聲明變量和函數。謝謝!! –

+0

@JeremyKellogg當然可以!歡迎您的朋友,如果您有任何問題隨時與我聯繫,我很樂意提供幫助。 –

-1

你不有申報爲你的功能,因此使用它是這樣的:

/* 
* 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; 
} 
+0

幫助某人獲得-1,你的人是令人難以置信的。 –

1

您聲明main()功能的範圍內printLine()功能。 welcomeScreen()的定義沒有看到該聲明。

移動printLine之外的聲明mainwelcomeScreen

之前同樣應與welcomeScreen

+0

@ user4581301是真的,只是編輯了我的答案,謝謝:) – Rogus

+0

您的回答是,並且是正確的。它只是有改進的餘地。我不知道爲什麼它被壓低了。 – user4581301

+0

我既不,但你能做什麼:)我看到任何人回答這個問題得到downvoted – Rogus

2

您的printLine函數只在main中聲明,所以welcomeScreen函數看不到它。

您應該在main之前移動welcomeScreen和printLine函數,並確保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; 
}