如何运行多个源文件???需要帮助(C++ CODEBLOCKS)

Sur*_*rya 1 c++ project codeblocks

我在一个项目的同一组源下有两个不同的.cpp文件(链表).我尝试运行一个名为"customer"的链接列表文件,但它只运行另一个名为"video"的文件.如何运行"客户"链表文件?

我的customer.cpp文件处于活动状态,但它仍在运行"视频"链接列表文件的程序.

基本上我试图带两个单独的客户列表和另一个单独的视频列表.

但是,当我尝试在customer.cpp选项卡下执行程序时,我认为应该运行该程序但运行video.cpp文件...我在这里遗漏了什么?

   #include <iostream>
    using namespace std;

    struct video
    {
      chartitle[40],star1[20],star2[20],star3[20],star4[20],prod[20],dir[20],proco[40];
     int copy;
     video *next;
     };
        video *first = NULL, *current = NULL;
      int optn = 0;
Run Code Online (Sandbox Code Playgroud)

^这是我的视频列表video.cpp文件的节点结构

      #include <iostream>
      using namespace std;

       struct customer
      {
       char f_name[20],l_name[20];
        int acc_num;
       customer *next;
        };
        customer *start = NULL, *pointer = NULL;
         int option = 0;
Run Code Online (Sandbox Code Playgroud)

^这是我的客户链表的节点结构.customer.cpp文件.这些文件在同一个项目下的两个单独的源文件中.

       int main(void)
        {
       first = NULL;
        current = NULL;
        do
       {
        display();
        cout << endl;
        cout << "Choose an option: " << endl;
        cout << "1. Move the current position forward once." << endl;
       cout << "2. Move the current position backwards once." << endl;
       cout << "3. Add a video at the beginning of the list." << endl;
        cout << "4. Add a video at the current position of the list." << endl;
       cout << "5. Add a video at the ending of the list." << endl;
       cout << "6. Delete the first video from the list." << endl;
       cout << "7. Delete the video at current position from the list." << endl;
       cout << "8. Delete the last video from the list." << endl;
       cout << "9. End program." << endl;
       cout << endl << " >> " ;
       cin >> optn;
     switch (optn)
    {
        case 1 : currentfor();
        break;
        case 2 : currentbac();
        break;
        case 3 : addbeginning();
        break;
        case 4 : addmiddle();
        break;
        case 5 : addending();
        break;
        case 6 : deletebegin();
        break;
        case 7 : deletemiddle();
        break;
        case 8 : deleteend();
        break;
    }
}
while (optn != 9);
Run Code Online (Sandbox Code Playgroud)

}

^这是我调用video.cpp文件的所有函数的代码.

 int mains(void)
 {
 start = NULL;
 pointer = NULL;
do
  {
    display_menu();
    cout << endl;
    cout << "Choose an option: " << endl;
    cout << "1. Move the current position forward once." << endl;
    cout << "2. Move the current position backwards once." << endl;
    cout << "3. Add a customer at the beginning of the list." << endl;
    cout << "4. Add a customer at the current position of the list." << endl;
    cout << "5. Add a customer at the ending of the list." << endl;
    cout << "6. Delete the first customer from the list." << endl;
    cout << "7. Delete the customer profile at current position from            the         list." << endl;
    cout << "8. Delete the last video from the list." << endl;
    cout << "9. End program." << endl;
    cout << endl << " >> " ;
    cin >> option;
     switch (option)
    {
        case 1 : current_forward();
        break;
        case 2 : current_backward();
        break;
        case 3 : add_beginning();
        break;
        case 4 : add_middle();
        break;
        case 5 : add_ending();
        break;
        case 6 : delete_beginning();
        break;
        case 7 : delete_middle();
        break;
        case 8 : delete_ending();
        break;
    }
}
while (option != 9);
Run Code Online (Sandbox Code Playgroud)

}

^这是我调用customer.cpp文件的所有函数的最终代码...当我最初尝试使用int main(void)为customer.cpp时,编译器显示错误,说两个视频中都声明了"main" .cpp和customer.cpp所以我尝试将"main"更改为"mains"然后它显示任何错误...我在这里错过了什么?

Mah*_*esh 5

我认为你假设项目中的每个源文件都需要一个main()函数.main()是执行可执行文件的起点.因此,整个可执行文件应该只有一个main().

编辑1

只编译源文件.此外,源文件应该通过三个阶段(预处理器 - >编译器 - >链接器).我会试着让你知道如何拆分.假设我们有两个源文件和一个标题 -

  1. main.cpp中
  2. Foo.cpp中
  3. foo.h中

将文件命名为驻留main.cpp位置是一种自定义main().现在 -

foo.h:通常声明都在这里.

class foo
{
    int number ;

    public:
    foo( int n );
    int getNumber() const;
};
Run Code Online (Sandbox Code Playgroud)

foo.cpp:因为,foo.cpp是一个源文件,它被编译.现在,要定义成员函数的定义,您需要包含它的标题.只是没有它,如果你定义foo成员函数,编译器不知道是什么foo.

#include "foo.h"

foo::foo( int n )
{
    number = n;
}

int foo::getNumber() const
{
    return number;
}
Run Code Online (Sandbox Code Playgroud)

在编译器之前,预处理器将内容复制foo.h到源文件foo.cpp.现在,我打算foo在我的实例中实例化main.cpp.现在,main.cpp不知道是什么foo.每个来源都是独立的.了解一个源文件并不意味着所有源文件都知道它.所以,你应该包括foo.hmain.cpp太.如果你试图创建一个对象foo,源文件main.cpp不知道标识符的foo含义.所以,

#include "foo.h"

int main()
{
    foo obj(10);
    obj.getNumber();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

g ++ main.cpp foo.cpp -o a.out.现在,我的a.out是可执行文件,其执行起点来自main()main.cpp.希望它在某种程度上有所帮助.