2012-04-29 28 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; 
} 

私はその後、私は、これは

cal.parallelplugin.cppファイルです else if(selection == "5"...

にリンクされ、このファイルを作成

#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

2つのmain機能があります。あなたは1つだけmain機能を持っている必要があります。

+0

ああ馬鹿だね:L – georgeherby

+0

@georgeherby:また、.cppファイルを 'インクルードする 'べきではありません。一般的には、ヘッダーファイルのみをインクルードするべきです。 –

3

あなたは2つのmain機能、

を持っているという事実だけでなく、私はあなたがC++でどのように使用されるか、複数のファイルを誤解していると思います。 1つの.cppファイルは、コンパイル中に別の.cppの内容を表示しません。

.cppファイルで関数(またはクラスなど)を定義することはできますが、別のファイルで参照するために.h(または.hpp/.hh)ファイルで宣言する必要があります。

たとえば、Utils.cppというファイルにint Test()という名前の関数があるとします。あなたのmain.cppファイルでこれを使用するには、Utils.hのライン(実際の名前は何の意味もありませんが、あなたが理解できる何かをしたい)に沿って何かという名前のファイルを作成する必要があります。 、このファイルには、単にテストと呼ばれる機能は、私のプログラムのどこかで宣言されているコンパイラに指示します

#pragma once 

int Test(); 

それはリンカの仕事です(に沿って、それが存在するという事実を受け入れ、コンパイルを動かす:Utils.h内には、のようなものを置く必要がありますコンパイラではなく、これらの問題を整理するため)。

次に、main.cppファイルにUtils.hを含めます。あなたは回、オブジェクトファイルのサイズをコンパイル増やしてしまうので、あなたはそれが一般的に考えられています悪い考えを.CPPファイルを含めることができますし、何かが二回解析される場合は、リンカの問題が発生するかもしれないが(私がいない最後の点でそう確信している)