2011-03-01 33 views
1

我在一個項目的同一組源文件下有兩個不同的.cpp文件(鏈接列表)。我嘗試運行一個名爲「customer」的鏈接列表文件,但它只運行另一個名爲「video」的鏈接列表文件。我如何運行「客戶」鏈接列表文件?如何運行多個源文件???需要幫助(C++代碼塊)

我的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; 

^這是我nodestructure的視頻列表中的文件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; 

^這是我對這些客戶的聯繫list.the customer.cpp中的文件。無論nodestructure有兩種在同一個項目下分離源文件。

 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中的文件...當我與詮釋主要(無效)最初嘗試,編譯器顯示在video.cpp和customer.cpp中聲明瞭「main」的錯誤,所以我嘗試將「main」改爲「mains」,然後顯示任何錯誤...我在這裏錯過了什麼?

+1

你爲什麼複製和粘貼*完全相同的問題*這一個? http://stackoverflow.com/questions/5150047/when-i-have-two-cpp-files-from-the-same-set-of-source-files-in-codeblocks-how你被告知不要做到這一點之前:http://stackoverflow.com/questions/5128613/here-is-my-entire-code-for-a-double-linked-list-c-help-needed-dont-know-where – 2011-03-01 04:50:11

回答

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; 
}; 

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; 
} 

克++ main.cpp中Foo.cpp中-o的a.out。現在,我的a.out是其執行起始點從main.cpp中定義的main()的可執行文件。 希望它在一定程度上有所幫助。

+0

所以什麼這是否意味着...?我應該從哪裏刪除main()?如果其中一個沒有main(),我將如何調用其中一個函數? – Surya 2011-03-01 04:22:50

+0

@Ovas - 這不像刪除'main()'。你需要明白,當你使用變量或調用函數時,變量/函數應該在範圍內。 – Mahesh 2011-03-01 04:51:46

0

好吧。

程序只能有一個main()函數。原因在於main是程序的「入口點」(它是運行時調用的第一個函數),所以如果您有多個函數,運行時將不知道從哪裏開始。爲了避免這種問題,如果定義了多個函數,鏈接器將引發錯誤。

現在,是否有一個原因,而他們是同一個源代碼樹的一部分?他們分享什麼嗎?如果不是,那麼只需分別編譯和鏈接它們。