2014-02-05 32 views
0

我有一項任務,必須爲我的函數使用不同的文件,而且我不必使用頭文件。問題是編譯器向我顯示了各種錯誤。所有的錯誤對我所有的功能都完全一樣。在不同文件中的函數定義中的結構參數

這是錯誤:

1> ------構建開始:項目:Asignacion 1,配置:調試的Win32 ------

1> SortCompany的.cpp

1> C:\用戶\伊曼紐爾\文件\視覺工作室2013 \項目\ asignacion 1 \ asignacion 1 \ sortcompany.cpp(1):錯誤C2065:Elemento的:未聲明的標識符

1 > c:\ users \ emanuel \ documents \ visual studio 2013 \ projects \ asignacion 1 \ asignacion 1 \ sortcompany.cpp(1):error C2146:syntax error:missing')'before'identifier'Dealer'

1> c :\ users \ emanuel \ documents \ visual studio 2013 \ projects \ asignacion 1 \ asignacion 1 \ sortcompany.cpp(1):error C2182:'SortCompany':非法使用類型'void'

1> c:\ users \ emanuel \ documents \ visual studio 2013 \ projects \ asignacion 1 \ asignacion 1 \ sortcompany.cpp(1):error C2059:syntax error:')'

1> c:\ users \ emanuel \ documents \ visual studio 2013 \ projects \ asignacion 1 \ asignacion 1 \ sortcompany.cpp(2):錯誤C2143:語法錯誤:缺少';'之前'{'

1> c:\ users \ emanuel \ documents \ visual studio 2013 \ projects \ asignacion 1 \ asignacion 1 \ sortcompany.cpp(2):error C2447:'{':missing function header(old式的正式名單?)

(同樣的錯誤如上的所有文件.....)

1>生成代碼... ==========構建:0成功,1失敗,0最新,0跳過==========


我有main.cpp這是主要文件(顯然)。主要調用「Menu.cpp」和菜單調用其他文件中的所有其他功能。

PD:Elemento的[]是一個結構

Menu.cpp代碼:

using namespace std; 

//these functions are fine 
void Closing(ifstream &, ofstream &); 
void Opening(ifstream &, ofstream &); 

//the problem is here with all these functions 
void Registro(Elemento[], int &, ifstream &); 
void InfoDealer(Elemento[], int, ofstream &); 
void SortCompany(Elemento[], int); 
void MayorVentas(Elemento[], int, ofstream &); 
void MayorVentasPorMarca(Elemento[], int, ofstream &); 



#include "Registro.cpp" 
#include "InfoDealer.cpp" 
#include "SortCompany.cpp" 
#include "MayorVentas.cpp" 
#include "MayorVentasPorMarca.cpp" 

void Menu() 
{ 
//code here... 
} 

Registro.cpp:

void Registro(Elemento Dealer[], int &Cantidad, ifstream &entrada) 
{ 
//code here... 
} 

main.cpp中:(此文件是默認的,我不應該改變它。)

//include stuff 
using namespace std; 

const int MAXIMODEALERS = 20; 
const int MAXIMOMODELOS = 6; 
struct Detail 
{ 
    string ModelName; 
    int Sales; 
}; 
typedef Detail Detalle; 

struct Element 
{ 
    string CompanyName; 
    Detalle Modelo[MAXIMOMODELOS]; 
}; 
typedef Element Elemento; 

Elemento Dealer[MAXIMODEALERS]; 

int Cantidad; 

void Menu(void); 


#include "Menu.cpp" 

void Opening(ifstream &Entrada, ofstream &Salida) 
{ 
//code 
} 

void Closing(ifstream &entrada, ofstream &salida) 
{ 
//code 
} 

int main() 
{ 
    Menu(); 
    return 0; 
} 

所有其餘的文件具有與Registro.cpp相同的結構

希望你能幫助我!如果你需要更多的細節,請提出要求。

+0

你確定這是一個* C++ *過程中,而不是* C *課程? – crush

+0

是C++ ......... – emanuel1337

+0

只是在開玩笑:P – crush

回答

0

您應該在使用之前轉發聲明類和結構。功能前添加struct Elemento;。無論如何,如果不包括ifstream,則無法使用ifstream

+0

是的,我在main.cpp中包含了所有庫。謝謝! – emanuel1337

+0

該結構已經在main.cpp中聲明 – emanuel1337

0

在翻譯單元中使用的每個名稱必須首先聲明,高於使用它的語句。例如,您的功能Registro正在使用名稱Elemento。編譯器不知道它,所以你必須首先聲明它在某處指定它的類型。這同樣適用於ifstreamofstream。另外,我認爲你正試圖鏈接Menu.cpp和其他文件中的函數的所有定義。那麼你遇到了麻煩,因爲每個函數都有多個定義,當你在Menu.cpp中使用include,而在每個源文件中都有另一個定義。當你鏈接所有這些時,你會得到每個函數的兩個定義。另外請注意,這是一個糟糕的設計。您絕不應該使用include.cpp文件。那是NOT,因爲你不被允許。預處理器不在乎您是否使用include.cpp.abcdef文件。他們都只是文本文件。有了這個,我的意思是你不應該在你的翻譯單元中包含definitions,因爲它們可能會導致你嚴重頭痛,而這是一個非常糟糕的設計,你不能指望。以下是如何組織代碼的一個很好的解釋。希望這可以幫助!

一些補充:

我看到你在下面評論。是的,Elementdefined(未聲明)在main.cpp中。但是,由於您正在編譯所有這些文件,因此編譯器不知道您指定的翻譯單元unless以外存在什麼內容。因此,如果您在使用前未使用declare,它將不知道名稱Element。我認爲你對一些概念有一些瞭解,比如:翻譯單元,預處理包括,連接,範圍,存儲時間等。花點時間閱讀它,讓一切變得更加清晰。

How to successfuly organize your source code in files.

相關問題