2012-04-29 275 views
2

我是一個初學者,我開始瞭解事情的竅門,但是我爲這個人添加了另一部分計算器。錯誤:C函數的聲明與之前的聲明衝突

我有這樣的main.cpp,我已經添加了位爲selection == 5

#include <iostream> 
#include <string.h> 

using namespace std; 

// Function includes 
// I try to keep them in the order they appear in the 
// output below for organization purposes 
#include "calc.m.xy12plugin.cpp" 
#include "calc.b.xymplugin.cpp" 
#include "calc.m.xybplugin.cpp" 
#include "calc.point.xymplugin.cpp" 
#include "calc.parallelplugin.cpp" 

// The above one would be here, too 

int main(int argc, const char* argv[]) { 
int i; 
i = 0; 
cout << "Linear Equation Calculator" << endl << "Copyright (c) 2011 Patrick Devaney" << endl 
<< "Licensed under the Apache License Version 2" << endl; 
// This loop makes the code a bit messy, 
// but it's worth it so the program doesn't 
// crash if one enters random crap such as 
// "zrgxvd" or "54336564358" 
while(i < 1) { 
cout << "Type:" << endl 
<< "0 to calculate a slope (the M value) based on two points on a line" << endl 
<< "1 to calculate the Y-intercept (the B value) based on two points and a slope" << endl 
<< "2 to calculate the slope (the M value) based on the Y-intercept and X and Y" << endl << 
"plug-ins" << endl 
<< "3 to find the next point up or down a line based on the slope (M) and X and Y" 
<< endl << "plug-ins" << endl 
<< "4 to find a point x positions down the line based on the slope (M) and X and Y" 
<< endl << "plug-ins" << endl 
<< "5 to find the equation of a parallel line in form y=mx+c" 
<< endl << "plug-ins" << endl; 

string selection; 
cin >> selection; 
if(selection == "0") { 
mcalcxyplugin(); 
i++; 
} 
else if(selection == "1") { 
calcbxymplugin(); 
i++; 
} 
else if(selection == "2") { 
calcmxybplugin(); 
i++; 
} 
else if(selection == "3") { 
calcpointxymplugin(1); 
i++; 
} 
else if(selection == "4") { 
int a; 
cout << "How many points up/down the line do you want? (Positive number for points" << endl 
<< "further up, negative for previous points" << endl; 
cin >> a; 
calcpointxymplugin(a); 
i++; 
} 
else if(selection == "5"){ 
calcparallelplugin(); 
i++; 
} 
else { 
i = 0; 
} 
// End of that loop below 
} 
return 0; 
} 

然後我創造了這個文件,我鏈接到else if(selection == "5"...

這是cal.parallelplugin.cpp文件

#include <iostream> 
using namespace std; 


int main(){ 
cout <<"Welcome to the Parallel Line Calculator \n" << endl; 
cout << "Here you will find the equation of the line parallel to a line passing through \na point (x,y) in the form y=mx+c \n\n" << endl; 

float x,y, c, x1, y1, gradient, c1; 

cout << "NOTE: Equation must be in form y=mx+c \n" << endl; 
cout <<"Please enter the number of Xs:" <<endl; 
cin >> x; 
cout <<"Please enter the number of Ys:" <<endl; 
cin >> y; 
cout <<"Please enter the number of Cs:" <<endl; 
cin >> c; 
cout <<"Please enter the x co-ordinate:" <<endl; 
cin >> x1; 
cout <<"Please enter the y co-ordinate:" <<endl; 
cin >> y1; 

gradient= x/y; 
c1 = y1 + (gradient*x1); 

cout << "Equation of parallel line through (" << x1 << ", " << y1 << ") is " << "y=" << gradient << "x+" << c1 << endl; 

} 

編譯這個時不會收到錯誤,但是當我編譯main.cpp時,我得到以下錯誤,我不能用我所有的生活工作了什麼是錯誤的:(

C:\Users\George\Desktop\linear_equation_calc\main.cpp||In function 'int main(int, const char**)':| 
C:\Users\George\Desktop\linear_equation_calc\main.cpp|51|error: declaration of C function 'int main(int, const char**)' conflicts with| 
C:\Users\George\Desktop\linear_equation_calc\calc.parallelplugin.cpp|9|error: previous declaration 'int main()' here| 
C:\Users\George\Desktop\linear_equation_calc\main.cpp||In function 'int main(int, const char**)':| 
C:\Users\George\Desktop\linear_equation_calc\main.cpp|100|error: 'calcparallelplugin' was not declared in this scope| 
||=== Build finished: 3 errors, 0 warnings ===| 

HELP!

+0

從外觀上看,你應該使用**功能**,而不是**文件**! – chris

回答

5

您有兩個main函數。您應該只有一個main函數。

+0

哦,是啊愚蠢的我:L – georgeherby

+0

@georgeherby:另外,你不應該'#inc'' ing .cpp文件;一般來說,你只應該包含'#include'頭文件。 –

3

除了這個事實,你有兩個main功能,

我想你是誤會文件如何在多個C的使用++。一個.cpp文件在編譯期間不應該看到另一個.cpp的內容。

您可以在.cpp文件中定義一個函數(或類等),但是您還必須在.h(或.hpp/.hh)文件中聲明該文件,然後使用該文件在另一個文件中引用它。

例如,如果你有一個名爲int Test()在一個名爲Utils.cpp文件的功能。要在您的main.cpp文件中使用此文件,您需要創建一個名爲Utils.h(實際名稱無關緊要,但您希望可以理解的內容)的文件。裏面Utils.h你應該把這樣的:

#pragma once 

int Test(); 

該文件只告訴了一個名爲測試功能在我的程序某處聲明編譯器,接受它的存在的事實,並沿着移動彙編(它是連接器的工作,而不是編譯器,以解決這些問題)。

然後在你的main.cpp文件中包含Utils.h。雖然,因爲你最終會增加編譯時間,對象文件大小可以包含.cpp文件,它通常被認爲是一個壞主意,如果事情被解析兩次,你可能會遇到的問題連接(我不是最後一個點那麼肯定)