错误:C函数的声明与先前的声明冲突

时间:2012-04-29 21:02:46

标签: c++ codeblocks linear-algebra calculator

我是一个初学者,我开始掌握一些事情,但我正在为这个人添加另一部分给这个计算器。

我将此作为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!

2 个答案:

答案 0 :(得分:5)

您有两个main个功能。您应该只有一个main功能。

答案 1 :(得分:3)

除了你有两个main函数,

我认为你误解了C ++中如何使用多个文件。在编译期间,一个.cpp文件不应该看到另一个.cpp的内容。

您可以在.cpp文件中定义函数(或类等),但是您还必须在.h(或.hpp / .hh)文件中声明它,然后将该文件用于在另一个文件中引用。

例如,如果在名为Utils.cpp的文件中有一个名为int Test()的函数。要在main.cpp文件中使用它,您需要创建一个名为Utils.h的文件(实际名称是无关紧要的,但您需要一些可以理解的内容)。在Utils.h内,您应该输入类似的内容:

#pragma once

int Test();

这个文件只是告诉编译器在我的程序中某个地方声明了一个名为Test的函数,接受它存在的事实并移动编译(这是链接器的工作,而不是编译器,以解决这些问题)。 / p>

然后在main.cpp文件中,添加Utils.h。虽然你可以包含.cpp文件,但它通常被认为是一个坏主意,因为你最终会增加编译时间,目标文件大小,如果解析了两次(在最后一点我不太确定),你可能会遇到链接器问题。

相关问题