C++ C++;如何将代码拆分为多个文件?

C++ C++;如何将代码拆分为多个文件?,c++,C++,我想把我的大程序分成多个文件。。。 也许只有一个结构, 然后使用函数读取csv文件,提取单词并将其放入数组中。。 然后使用另一个函数对数组进行bubblesort排序, 最后是我把所有东西放在一起的主要功能。。。 问题是,我在主函数的开头声明了必要的变量,当我想分离函数时,我在主函数中声明的变量以及我用于这些函数的变量都不再存在了。。。我必须再次将它们作为参数传递给函数吗?还是有其他/更好的方法?我可以声明外部变量还是静态变量 其次,我不知道何时制作一个.h文件或另一个.cpp文件,如果是另一个

我想把我的大程序分成多个文件。。。 也许只有一个结构, 然后使用函数读取csv文件,提取单词并将其放入数组中。。 然后使用另一个函数对数组进行bubblesort排序, 最后是我把所有东西放在一起的主要功能。。。 问题是,我在主函数的开头声明了必要的变量,当我想分离函数时,我在主函数中声明的变量以及我用于这些函数的变量都不再存在了。。。我必须再次将它们作为参数传递给函数吗?还是有其他/更好的方法?我可以声明外部变量还是静态变量

其次,我不知道何时制作一个.h文件或另一个.cpp文件,如果是另一个.cpp文件,它不允许有另一个主功能,那么我该怎么做?声明一个公共类

以下是我要分离的代码:

#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
//#include "studentStruct.h"

using namespace std;    


struct Stud{
        long matrnr;
        char vorname[30];
        char name[30];
        char datum[30];
        float note;
    };


void bubbleSort(Stud mystud[], int studentCounter);             


int main(int argc, char **argv)
{   

    const int MAX = 30;
    Stud stud;  
    Stud mystud[30]; // <<-- Array of "Stud" type
    //memset((void*)mystud,0,sizeof(mystud) * sizeof(Stud));
    int wordCounter(0);
    int i(0); //thats the charCounter or index
    int studentCounter(0);
    char wort[MAX];



  FILE * pFile;
  int cnr(0);     

  pFile=fopen("studentendaten.txt","r");  
  if (pFile==nullptr) 
  {
      perror ("Fehler beim öffnen der Datei");
  }

  else
  {       
    while (cnr != EOF) 
    {       
        (cnr=fgetc(pFile)) ;    

        if ((char)cnr == '\n') {
            mystud[studentCounter] = stud;
            studentCounter++;                       
            continue;           
        }

        if ((char)cnr == ';') { 


            wort[i] = '\0'; 

            switch (wordCounter % 5) {

                case 0:             
                stud.matrnr = atol(wort);
                break;

                case 1:
                strcpy(stud.name, wort);
                break;

                case 2:
                strcpy(stud.vorname, wort);
                break;

                case 3:
                strcpy(stud.datum,wort);
                break;

                case 4:
                stud.note = atof(wort); 
                break;
            }           

            wordCounter++;          
            i = 0;
            continue;
        }

        wort[i] = (char)cnr;
        i++;                

    }   

    mystud[studentCounter] = stud;
    fclose (pFile);
}


    bubbleSort(mystud , studentCounter);

    for (int i(0) ; i <= studentCounter; i++) {
    //cout <<mystud[i].matrnr << "  |  " << mystud[i].name << " |  " << mystud[i].vorname <<"  |  " 
    //<< mystud[i].datum <<"  |  " << mystud[i].note << endl;
    printf("%ld %15s    %15s    %15s    %6.1f\n",mystud[i].matrnr,mystud[i].name,mystud[i].vorname,mystud[i].datum,mystud[i].note);  
    }

    return 0;
}

void bubbleSort(Stud mystud[], int studentCounter) {
    Stud tmp;   
    for (int i = 0 ; i<= studentCounter; ++i) {
        for (int j=0; j<= studentCounter-1;  ++j) {
            if (mystud[j].note > mystud[j+1].note) 
            {
                /**
                tmp = mystud[j+1];
                mystud[j+1] = mystud[j];
                mystud[j] = tmp;
                **/

                    tmp.matrnr = mystud[j+1].matrnr;
                    strcpy(tmp.vorname,mystud[j+1].vorname);
                    strcpy(tmp.name,mystud[j+1].name);
                    strcpy(tmp.datum , mystud[j+1].datum);
                    tmp.note = mystud[j+1].note;

                    mystud[j+1].matrnr = mystud[j].matrnr;
                    strcpy(mystud[j+1].vorname ,mystud[j].vorname);                 
                    strcpy(mystud[j+1].name , mystud[j].name);
                    strcpy(mystud[j+1].datum ,mystud[j].datum);
                    mystud[j+1].note = mystud[j].note;

                    mystud[j].matrnr = tmp.matrnr;
                    strcpy(mystud[j].vorname , tmp.vorname);
                    strcpy(mystud[j].name , tmp.name);
                    strcpy(mystud[j].datum , tmp.datum);
                    mystud[j].note = tmp.note;


                }           
            }
        }       
    }
#包括
#包括
#包括
#包括
#包括
//#包括“studentStruct.h”
使用名称空间std;
结构螺柱{
长matrnr;
char-vorname[30];
字符名[30];
字符数据[30];
浮动票据;
};
void bubbleSort(Stud mystud[],int studentCounter);
int main(int argc,字符**argv)
{   
常数int MAX=30;
螺柱;
Stud mystud[30];//
  • 一个头文件说-我们可以这样做
  • 一个.cpp文件说-我们就是这样做的-即编译成一个对象
.cpp文件使用头文件来了解可用的文件

编译.cpp文件。生成目标文件。将它们链接在一起,这样就可以解决问题。砰-得到一个可执行文件。

最好(也是标准的)做法可能是:

  • 编写一个类Stud,而不是结构(类的私有成员将是
    name
    vorname
    等)
  • 创建一个头文件,在其中编写类的定义以及所有函数的原型
  • 在单独的.cpp文件中定义这些函数
  • 最后,您只需为实现创建另一个.cpp文件(main)
  • 当然,您需要在.cpp文件中包含头文件