如何运行多个源文件???需要帮助(C++ 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;
^这是我的视频列表的节点结构 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 文件的所有函数的最终代码...当我最初尝试使用 customer.cpp 的 int main(void) 时,编译器显示一个错误,指出“main”已在两个文件中声明video.cpp 和 customer.cpp 所以我尝试将“main”更改为“mains”,然后它没有显示任何错误......我在这里错过了什么?
I have two different .cpp files (linked lists) under the same set of source of one project. I tried running one of the linked list files called "customer", but it's only running the other one called "video". How can I run the "customer" linked list file?
My customer.cpp file is active but it's still running the program for the "video" linked list file.
Basically Im trying to bring two seperate lists of customers and another seperate list with videos.
But when I try executing the program under the customer.cpp tab I thought it was supposed to run that but its running the video.cpp file...am I missing something here?
#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;
^this is my nodestructure for the video list the video.cpp file
#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;
^this is my nodestructure for customer linked list.the customer.cpp file .both of these are in two seperate source files under the same project.
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);
}
^this is the code where i call all the functions for the video.cpp file.
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);
}
^this is the final code where i call all functions for customer.cpp file... when I tried initially with int main(void) for the customer.cpp,the compiler showed an error saying that "main" was declared in both video.cpp and customer.cpp so I tried changing "main" to "mains" then it dint show any error...what did I miss here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您假设项目中的每个源文件都需要一个
main()
函数。main()
是可执行文件执行的起点。因此,整个可执行文件应该只有一个main()
。编辑 1
仅编译源文件。此外,源文件应经过三个阶段(预处理器 -> 编译器 -> 链接器)。我将尝试让您了解如何拆分。假设我们有两个源文件和一个头文件 -
习惯上将文件命名为
main.cpp
wheremain()
驻留。现在 -foo.h:通常声明放在这里。
foo.cpp:由于
foo.cpp
是源文件,因此它会被编译。现在,要定义成员函数的定义,您需要包含它的标头。如果没有它,如果您定义foo
成员函数,编译器不知道foo
是什么。在编译之前,预处理器将 foo.h 的内容复制到源文件 foo.cpp 中。现在,我打算在我的
main.cpp
中实例化foo
。现在,main.cpp
不知道foo
是什么。每个来源都是独立的。知道一个源文件并不意味着所有源文件都知道它。因此,您也应该在main.cpp
中包含foo.h
。如果没有它,如果您尝试为foo
创建对象,源文件main.cpp
不知道标识符foo
的含义。所以,g++ main.cpp foo.cpp -o a.out。现在,我的 a.out 是可执行文件,其执行起点是
main.cpp
中定义的main()
。希望它能在一定程度上有所帮助。
I think you are under assumption that each source file in the project requires a
main()
function.main()
is the starting point of execution of an executable. So, the entire executable should have only onemain()
.Edit 1
Only source files get compiled. Also, there are three stages which source files should pass through ( Preprocessor -> Compiler -> Linker). I will try to give you an idea of how to split. Assume that, we have two source files and a header -
It is a custom to name the file as
main.cpp
wheremain()
resides. Now -foo.h: Usually declarations go here.
foo.cpp: Since,
foo.cpp
is a source file, it gets compiled. Now, to define the definitions of member functions, you need to include it's header. Just with out it, if you definefoo
member functions, compiler doesn't know whatfoo
is.Before the compiler, the preprocessor copies the content of
foo.h
to the source filefoo.cpp
. Now, I intend to instantiatefoo
in mymain.cpp
. Now,main.cpp
doesn't know whatfoo
is. Each source is independent. Knowing to one source file doesn't mean that all the source files know it. So, you should includefoo.h
inmain.cpp
too. With out it, if you try to create an object forfoo
, the source filemain.cpp
doesn't know what the identifierfoo
means. So,g++ main.cpp foo.cpp -o a.out. Now, my a.out is the executable whose starting point of execution is from
main()
defined inmain.cpp
.Hope it helps to an extent.
出色地。
一个程序只能有一个
main()
函数。原因是main
是程序的“入口点”(它是运行时调用的第一个函数),因此如果您有多个函数,运行时将不知道该去哪里开始。为了避免此类问题,如果多次定义main
函数,链接器将引发错误。现在,它们属于同一源树的一部分是否有原因?他们有分享什么吗?如果没有,那么只需分别编译和链接它们即可。
Well.
You can only have one
main()
function by program. And the reason is thatmain
is the "entry point" for your program (it's the first function that's called by the runtime), so if you have more than one, the runtime won't know where to start. To avoid such problems, the linker will raise an error if amain
function is defined more than once.Now, is there a reason while they're part of the same source tree? Do they share anything? If not, then just compile and link them separately.
这不是问题的答案,而是仅适用于那些实际需要在 Code::Blocks 中运行多个
.c
或.cpp
文件的人。不要使用项目,而是从
“文件 > 关闭项目”
关闭项目,现在打开您的文件,您可以单独运行它们,它就会工作。编译器将在打开文件的文件夹中构建文件,因此您将在同一文件夹中获得至少 2 个文件(扩展名为“
*.exe”
和“*.o”< /code> 对于 Windows)。
This is not an answer to the question, rather this is only for those who actually need to run multiple
.c
or.cpp
files in Code::Blocks.Instead of having a project, close the project from
"File > Close project"
, now open your files and you can run those separately and it will work.The compiler will build the files your the folder of the opened file, so you will get atleast 2 files in the same folder (with extensions "
*.exe"
and"*.o"
for Windows).