需要为原型函数创建单独的cpp文件

时间:2015-07-31 07:42:21

标签: c++

我需要创建一个单独的cpp文件来提取函数,我的书和power point slide没有详细介绍。请帮忙。

"在单独的.cpp文件中包含至少一个函数。"

我知道这是格式,但我失去了怎么做。 对于每个函数参数, 原型必须在括号内包含每个参数的数据类型 标头必须包含其()

中每个参数的声明
void evenOrOdd(int);  //prototype
void evenOrOdd(int num) //header
evenOrOdd(val);       //call
#include <iostream>
#include <iomanip>
#include <fstream>
#include <limits>
using namespace std;               



int main()
{
char again;// assigned variable for run again loop
do
 {
   int n;  //n=number of computers
   int conn;// conn= total of connections

   cout <<"\t*********************************;
   cout <<******************************\n";
   cout << "\t*This program will display a list ;
   cout << of how many possible network*\n";
   cout << "\t*connections you could have;
   cout << given any number of computers.  *\n";
   cout << "\t*********************************;
   cout <<******************************\n";
   cout << "\n";
   cout << "Enter the maximum number of computers\n";
   cout <<for which you would like to;
   cout <<calculate total connections: ";
   cin >> n;


         //validate the input
    while (!cin || n <2 )
{
        //Explain the invalid input.

        cin.clear();
        cin.ignore(numeric_limits <streamsize> ::max(), '\n');
        cout << "You must enter a number range from 2-10\n";
        cout << "Please enter your selection again: \n";
        cin >> n;
}   
    //Block that will open file, write to file, and display to the console
    cout << "\n";
    ofstream myFile;
    myFile.open("totalComputers.txt");
    cout << "Number of Computers\tTotal Connections\n";
    myFile << "Number of Computers\tTotal Connections\n";
    cout << "////////////////////////////////////////\n";
    myFile << "////////////////////////////////////////\n";


    for (int count=2; count <= n; count++)
        {

            conn = count * (count - 1) / 2;
            cout << setw(10) << count << setw(24) << conn << endl;
            myFile << setw(10) << count << setw(24) << conn << endl;

        }
          //This was implemented to close the file, 
          //and notify user that data write is complete.
            myFile.close();
            cout << "\n";
            cout << "The data file has finished writing.\n";




     //This will allow the user to run the program again or exit the program
       cout << "\n";
       cout << "Would you like to run the program again? y or n\n";
       cin >> again;
}      while (again == 'y');
       cout << "Have a great day, please press any key to exit.\n";

return 0;

}

1 个答案:

答案 0 :(得分:0)

我不太确定我是否正确理解了你的问题。我假设您要做的就是在不同的cpp文件中实现evenOrOdd。 (我还要回复int,因为如果它返回无效,我不会理解该函数会做什么 考虑这是一个(几乎)如何拥有多个cpp文件的最小例子。

此示例由三个文件组成:foo.h,foo.cpp和main.cpp

<强> foo.h中:

/*
 * This file contains the declaration. Prototype
 */

int evenOrOdd(int); //prototype

foo.cpp (这是&#34;在一个单独的.cpp文件中包含至少一个函数。&#34; -file):

/*  
 *  This file contains the implementation
 */
#include "foo.h"

//returns 0 if even and 1 if odd
int evenOrOdd(int n){
    return n%2;
}

<强> main.cpp中:

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

int main(){
    int val = 5;
    std::cout << evenOrOdd(val) << "\n";
    return 0;
}

然后编译如下: 第一次运行

g++ -c foo.cpp

这会生成对象文件foo.o. 然后使用以下命令将主链接foo.o编译到其中:

g++ main.cpp foo.o -o main

并使用

运行
./main

由于val为5而5%2为1,因此应输出1。

我不确定这正是您所需要的,但我希望它能让您了解如何解决您的问题。

注1:在本例中,我没有遵循最佳实践(例如标题保护),以保持最低限度。

注意2:我在cpp文件中实现了evenOrOdd,因为我理解你的任务要求你这样做,但你可以直接在foo.h中实现它,然后编译就是:< / p>

g++ main.cpp -o main

一些参考材料: