簡體   English   中英

如何從單獨的 .cpp 文件調用函數?

[英]How to call a function from a separate .cpp file?

所以這是我試圖解決的原始問題;

*創建一個名為 Lab5_Figures 的項目。 該項目應包含多個文件。 編寫一個程序,反復要求用戶選擇正方形、左三角形或直角三角形,然后輸入圖形大小,然后以星形打印適當的形狀。 對於正方形,程序應該詢問用戶是想要填充正方形還是空心正方形。 如果用戶輸入無效選項,程序應該退出。 請參閱下面的示例對話框:

  1. 正方形
  2. 左下三角
  3. 右上角三角形

選擇圖:1

選擇尺碼:4

填充或空心 [f/h]: h

//打印出合適的圖形然后重復

  1. 正方形
  2. 左下三角
  3. 右上角三角形...

您可以重用循環實驗室中的代碼(我已經這樣做了)。 將星形打印代碼放置在四個單獨的函數中:fillSquare、hollowSquare、leftTriangle 和 rightTriangle。 每個函數都應該接受一個整數參數——圖形的大小並且不返回任何值(是一個空函數)。 創建三個單獨的文件figures.cpp、figures.h 和figuresInput.cpp。 將三角形和正方形函數定義放在figures.cpp 中,並將它們的原型放在figures.h 中。 確保頭文件受到保護以防止多次包含。 將 main 函數放在 figureInput.cpp* 中。

好吧,爽。 現在這是我的文件; (如果我的格式關閉,我深表歉意:()

數字輸入.cpp

// This program creates shapes based on user input


#include <iostream>
#include <string>
#include "figures.h"

using std::cin; using std::cout; using std::string; using std::endl;

int main()
{
    int option = 1;

while (option == 1 || option == 2 || option == 3)
{
    //determine choice
    cout << "1. Square" << endl << "2. Left Triangle" << endl << "3. Right Triangle" << endl;
    cout << "Select an option: ";

    cin >> option;

    if (option == 1)
    {
        char fillHollow;

        cout << "Filled or hollow? [f/h]";
        cin >> fillHollow;

        if (fillHollow == 'f')
        {
            int size;

            cout << "Input Size: ";
            cin >> size;

            void filledSquare(int size);
        }

        else if (fillHollow = 'h')
        {
            int size;

            cout << "Input Size: ";
            cin >> size;

            void hollowSquare(int size);
        }
    }

    else if (option == 2)
    {
        int size;

        cout << "Input Size: ";
        cin >> size;

        void leftTriangle(int size);
    }

    else if (option == 3)
    {
        int size;

        cout << "Input Size: ";
        cin >> size;

        void rightTriangle(int size);
    }

    else
        exit(EXIT_FAILURE);
}
} //end main

數字.cpp

//function defintions

#include "figures.h"
#include <iostream>

using std::cin; using std::cout; using std::string; using std::endl;

void filledSquare(int a)
{
//print stars for first square
for (int b = 0; b < a; b++)
{
    for (int c = 0; c < a; c++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line

} //end

void hollowSquare(int a)
{
for (int b = 0; b < a; b++)
{
    int spaces = a - 2;
    if (b == 0 || b == (a - 1))
    {
        for (int i = 0; i < a; i++)
            cout << "*";
        cout << endl;  //new line
    }
    else
    {
        cout << "*";
        for (int i = 0; i < spaces; i++)
            cout << " ";
        cout << "*";
        cout << endl;  //new line
    }
}
} //end

void leftTriangle(int a)
{
//get user input and print stars for first triangle
for (int b = a; b < a; b--)
{
    for (int c = 0; c < b; c++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line
} //end

void rightTriangle(int a)
{
//get user input and print stars for second triangle
for (int b = 0; b < a; b++)
{
    int stars = a - b;
    for (int i = 0; i < b; i++)
        cout << " ";
    for (int i = 0; i < stars; i++)
        cout << "*";
    cout << endl; //new line
}
cout << endl; //new line
} //end

最后是figures.h

//funtion prototypes

#ifndef FIGURES_H
#define FIGURES_H

void filledSquare(int);

void hollowSquare(int);

void leftTriangle(int);

void rightTriangle(int);

#endif;

好的,所以我認為我的問題是我沒有正確地從 main 調用函數定義。 我不確定我是否只是沒有包含正確的內容或什么; 我真的很感激我能得到的任何幫助。

這是我的輸出的樣子;

1. Square
2. Left Triangle
3. Right Triangle
Select an option: 1
Filled or hollow? [f/h]f
Input Size: 4
1. Square
2. Left Triangle
3. Right Triangle
Select an option:

您實際上並不是在調用函數,而是在聲明它們。

你這樣稱呼他們:

hollowSquare(size);

在您的示例中,您沒有調用 fillSquare 函數,但只要使用相同的 SIGNATURE 和 RETURN TYPE,您就會再次聲明它。 在這種情況下,編譯器不會抱怨,例如:

void foo(); // ok
void foo(); // ok
void foo(); // ok

上面所有這些原型對編譯器都是正確的,只要它們在所有方面都相同,但是您必須提供一個定義,因為它們是相同的函數原型。

void foo(); // ok
     foo(); // error foo differs only in return type which doesn't mean overloading it. this function without return type by default returns int
char foo(); // error foo differs only in return type.

上面的原型是不正確的,因為它們僅在返回類型上有所不同。 重載函數簽名必須在參數數量或參數類型(至少在一個參數中它們不同)或兩者不同。

void foo();        // ok first prototype with which the compiler compare the following
int  foo(int);     // ok differs in number and type of parameters
     foo(int&)     // ok differs in number and type of parameters
void foo(int, int) // ok differs in number and type of parameters

在您的示例中,您正在重新聲明 FilledSquare,只要它已經聲明。

void filledSquare(int size); // ??!! here you are not calling this function but you are declaring it instead.

調用函數只需將函數名稱放在里面,沒有返回類型和括號,如果需要任何參數,則在其中放置參數,但從不說明參數的類型。

filledSquare(size); // without return type nor type of parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM