C++ 函数出错

C++ Getting an error with my functions

需要一些作业帮助。我是 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' was not declared in this scope"。我确实在 main() 函数中声明了 "printLine()",这还不够吗?或者我是否需要在我计划使用它的每个函数中声明函数名称?为了回答这个紧迫的问题,我必须在这个最终项目中使用函数。谢谢!!!

你的函数没有声明,所以这样使用它:

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

您在 main() 函数的范围内声明了 printLine() 函数。 welcomeScreen().

的定义看不到这个声明

printLine 的声明移到 main 之外 welcomeScreen

之前

welcomeScreen

的声明也应如此

假设您真的不想在 main 中声明函数,您需要转发声明 welcomeScreen 和 printLine 函数:

#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
}

或者在 main 之前定义它们:

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

int printLine(int length) {
    //definition
}

void welcomeScreen() {
    // definition
}

int main () {
    welcomeScreen();
    return 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;
}