[英]How to call a function from a separate cpp file in the main.cpp file?
[英]How to call a function from a separate .cpp file?
所以这是我试图解决的原始问题;
*创建一个名为 Lab5_Figures 的项目。 该项目应包含多个文件。 编写一个程序,反复要求用户选择正方形、左三角形或直角三角形,然后输入图形大小,然后以星形打印适当的形状。 对于正方形,程序应该询问用户是想要填充正方形还是空心正方形。 如果用户输入无效选项,程序应该退出。 请参阅下面的示例对话框:
选择图:1
选择尺码:4
填充或空心 [f/h]: h
//打印出合适的图形然后重复
您可以重用循环实验室中的代码(我已经这样做了)。 将星形打印代码放置在四个单独的函数中: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.