2

我有点初学者,我开始掌握一些东西,但我正在为这个人添加另一个部分到这个计算器。

我有这个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 ===|

帮助!

4

2 回答 2

5

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

于 2012-04-29T21:05:50.043 回答
3

除了你有两个main功能之外,

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

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

例如,如果您int Test()在名为 Utils.cpp 的文件中调用了一个函数。要在您的main.cpp文件中使用它,您需要创建一个名为 something 的文件Utils.h(实际名称无关紧要,但您想要一些可以理解的东西)。在里面Utils.h你应该放一些类似的东西:

#pragma once

int Test();

该文件只是告诉编译器在我的程序的某处声明了一个名为 Test 的函数,接受它存在的事实并继续编译(解决这些问题是链接器的工作,而不是编译器)。

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

于 2012-04-29T21:13:16.430 回答