我在一个项目的同一组源代码下有两个不同的.cpp 文件(链表)。我尝试运行一个名为“customer”的链接列表文件,但它只运行另一个名为“video”的文件。如何运行“客户”链表文件?
我的 customer.cpp 文件是事件的,但它仍在运行“video”链表文件的程序。
基本上,我试图带来两个单独的客户列表和另一个单独的视频列表。
但是当我尝试在 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;
^这是我的视频列表 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;
^这是我的客户链表的节点结构。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);
^这是我调用 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);
^这是我为 customer.cpp 文件调用所有函数的最终代码...当我最初尝试使用 int main(void) 为 customer.cpp 时,编译器显示错误,指出“main”已声明在 video.cpp 和 customer.cpp 中,所以我尝试将“main”更改为“mains”然后它显示任何错误...我在这里错过了什么?
最佳答案
我认为您假设项目中的每个源文件都需要一个 main()
函数。 main()
是可执行文件执行的起点。因此,整个可执行文件应该只有一个 main()
。
编辑 1
只编译源文件。此外,源文件应该经过三个阶段(Preprocessor -> Compiler -> Linker)。我将尝试让您了解如何拆分。假设,我们有两个源文件和一个 header -
- main.cpp
- foo.cpp
- foo.h
习惯将文件命名为main.cpp
,其中main()
所在。现在-
foo.h:通常在这里声明。
class foo
{
int number ;
public:
foo( int n );
int getNumber() const;
};
foo.cpp:因为 foo.cpp
是一个源文件,它会被编译。现在,要定义成员函数的定义,您需要包含它的标题。只是没有它,如果你定义 foo
成员函数,编译器不知道 foo
是什么。
#include "foo.h"
foo::foo( int n )
{
number = n;
}
int foo::getNumber() const
{
return number;
}
在编译器之前,预处理器将foo.h
的内容复制到源文件foo.cpp
中。现在,我打算在我的 main.cpp
中实例化 foo
。现在,main.cpp
不知道 foo
是什么。每个来源都是独立的。知道一个源文件并不意味着所有源文件都知道它。因此,您也应该在 main.cpp
中包含 foo.h
。如果没有它,如果您尝试为 foo
创建对象,源文件 main.cpp
不知道标识符 foo
的含义.所以,
#include "foo.h"
int main()
{
foo obj(10);
obj.getNumber();
return 0;
}
g++ main.cpp foo.cpp -o a.out。现在,我的a.out 是可执行文件,其执行起点来自main.cpp
中定义的main()
。
希望能在一定程度上有所帮助。
关于c++ - 如何运行多个源文件???需要帮助(C++代码块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5150383/