简体   繁体   English

如何从单独的 .cpp 文件调用函数?

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

So here is the original question I am trying to solve;所以这是我试图解决的原始问题;

*Create a project titled Lab5_Figures. *创建一个名为 Lab5_Figures 的项目。 This project shall contain multiple files.该项目应包含多个文件。 Write a program that repeatedly asks the user to select either square, left or right triangle, then inputs the figure size and then prints the appropriate shape in stars.编写一个程序,反复要求用户选择正方形、左三角形或直角三角形,然后输入图形大小,然后以星形打印适当的形状。 For square, the program should ask whether the user wants a filled or a hollow square.对于正方形,程序应该询问用户是想要填充正方形还是空心正方形。 The program should quit if the user inputs an invalid option.如果用户输入无效选项,程序应该退出。 See an example dialog below:请参阅下面的示例对话框:

  1. square正方形
  2. bottom left triangle左下三角
  3. top right triangle右上角三角形

select figure: 1选择图:1

select size: 4选择尺码:4

filled or hollow [f/h]: h填充或空心 [f/h]: h

//print out appropriate figure then repeat //打印出合适的图形然后重复

  1. square正方形
  2. bottom left triangle左下三角
  3. top right triangle ...右上角三角形...

You can reuse your code from the Looping lab (I already did this).您可以重用循环实验室中的代码(我已经这样做了)。 Place star-printing code in four separate functions: filledSquare, hollowSquare, leftTriangle and rightTriangle.将星形打印代码放置在四个单独的函数中:fillSquare、hollowSquare、leftTriangle 和 rightTriangle。 Each function should accept a single integer parameter - the size of the figure and return no value (be a void-function).每个函数都应该接受一个整数参数——图形的大小并且不返回任何值(是一个空函数)。 Create three separate files figures.cpp, figures.h, and figuresInput.cpp.创建三个单独的文件figures.cpp、figures.h 和figuresInput.cpp。 Place the triangle and square function definitions in figures.cpp and their prototypes in figures.h.将三角形和正方形函数定义放在figures.cpp 中,并将它们的原型放在figures.h 中。 Make sure that the header file is protected against multiple inclusion.确保头文件受到保护以防止多次包含。 Place the main function in figuresInput.cpp*.将 main 函数放在 figureInput.cpp* 中。

Okay, cool.好吧,爽。 Now here are my files;现在这是我的文件; ( I apologize if my formatting is off :( ) (如果我的格式关闭,我深表歉意:()

figuresInput.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

figures.cpp数字.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

and finally figures.h最后是figures.h

//funtion prototypes

#ifndef FIGURES_H
#define FIGURES_H

void filledSquare(int);

void hollowSquare(int);

void leftTriangle(int);

void rightTriangle(int);

#endif;

Okay, so I think my problem is that I am not calling the function definitions from main correctly.好的,所以我认为我的问题是我没有正确地从 main 调用函数定义。 I'm not sure if I just didn't include something right or what;我不确定我是否只是没有包含正确的内容或什么; I would really appreciate any help I could get.我真的很感激我能得到的任何帮助。

Here is what my output looks like;这是我的输出的样子;

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:

You are not actually calling the functions, but declaring them.您实际上并不是在调用函数,而是在声明它们。

You call them like this:你这样称呼他们:

hollowSquare(size);

in your example you are not calling filledSquare function but you are declaring it again as long as with the same SIGNATURE and RETURN TYPE.在您的示例中,您没有调用 fillSquare 函数,但只要使用相同的 SIGNATURE 和 RETURN TYPE,您就会再次声明它。 in this scenario the compiler will not complain eg:在这种情况下,编译器不会抱怨,例如:

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

all these prototypes above are correct to the compiler as long as they are identical in everything BUT you must provide one definition only because they are the same function's prototype.上面所有这些原型对编译器都是正确的,只要它们在所有方面都相同,但是您必须提供一个定义,因为它们是相同的函数原型。

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.

the prototypes above are incorrect because they differ only in return type.上面的原型是不正确的,因为它们仅在返回类型上有所不同。 to overload a function signatures must differ in number of parameters or type of parameters(at least in one parameters they differ) or the two.重载函数签名必须在参数数量或参数类型(至少在一个参数中它们不同)或两者不同。

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

int your example you are re-declaring filledSquare as long as it is already declared.在您的示例中,您正在重新声明 FilledSquare,只要它已经声明。

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

to call a function just put the name of the function without return type and parenthesis inside which put parameters if it takes any BUT never state the TYPE of parameters.调用函数只需将函数名称放在里面,没有返回类型和括号,如果需要任何参数,则在其中放置参数,但从不说明参数的类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM