C++;错误:未定义对…的引用&;多重定义 我是C++的新手。我正在使用netbeanside。我创建了类 Point < /St>(点.h和点.CPP)和另一个C++文件 GEO.CPP我在其中放置了方法之后使用。我还创建了一个头文件,其中Igeo.h放置了在geo.cpp中实现的方法声明。我的代码如下: h点
借C++;错误:未定义对…的引用&;多重定义 我是C++的新手。我正在使用netbeanside。我创建了类 Point < /St>(点.h和点.CPP)和另一个C++文件 GEO.CPP我在其中放置了方法之后使用。我还创建了一个头文件,其中Igeo.h放置了在geo.cpp中实现的方法声明。我的代码如下: h点,c++,netbeans,C++,Netbeans,借 #包括“geo.h” 编译器没有给我任何错误,但是当我检查输出文件file.csv时,它仍然是空的 在创建文件(geo.h和geo.cpp)之前知道一切都很好 提前感谢。请删除已解决的中间错误 您需要在compile命令中包含geo.cpp: g++ main.cpp geo.cpp -o main.out < C++改进技巧>: 不要在头文件中使用“使用…” 在头文件的最顶端有include-guard 请删除已解决的中间错误 您需要在compile命令中包含geo.cpp: g++
#包括“geo.h”
编译器没有给我任何错误,但是当我检查输出文件file.csv
时,它仍然是空的
在创建文件(geo.h和geo.cpp)之前知道一切都很好
提前感谢。请删除已解决的中间错误 您需要在compile命令中包含geo.cpp:
g++ main.cpp geo.cpp -o main.out
< C++改进技巧>:
- 不要在头文件中使用“使用…”
- 在头文件的最顶端有include-guard
g++ main.cpp geo.cpp -o main.out
< C++改进技巧>:
- 不要在头文件中使用“使用…”
- 在头文件的最顶端有include-guard
名称空间。如果要使用命名空间,请首选命名空间{}
#如果不确定点(H)
#定义点
#包括
#包括
//代码
#恩迪夫
名称空间。如果要使用命名空间,请首选命名空间{}
#如果不确定点(H)
#定义点
#包括
#包括
//代码
#恩迪夫
坚持使用之前使用的行(
g++-o dist/Debug/GNU-Linux-x86/dgilog build/Debug/GNU-Linux-x86/Plan.o build/Debug/GNU-Linux-x86/Point.o…
)。如果您只包含一些文件,那么显然您将有未定义的引用,将其更改为char theFilename[]=…
;当我使用命名空间删除std时,我发现此错误geo.h:21:24:error:“vector”未在此范围内声明。是否必须向每个向量声明添加std::如下:std::vector
@MattMcNabb@Dinosaur对如果您使用的是STL类型,并且没有使用名称空间std
在代码中,您需要将std::
放在该类型的开头。例如,std::map
,std::vector
,std::string
,std::cout
,std::endl
。是的,我纠正了它。但也有同样的问题。我在编译过程中没有出现任何错误,但在运行project时,输出文件file.csv仍然是空的:(.请继续使用之前使用的行(g++-o dist/Debug/GNU-Linux-x86/dgilog build/Debug/GNU-Linux-x86/Plan.o build/Debug/GNU-Linux-x86/Point.o…
)。如果只包含部分文件,则显然会有未定义的引用。若要修复“不推荐的转换”警告,请在char*theFileName=“file.csv”
行中将其更改为char theFileName[]=…
;当我使用名称空间删除std时,我发现此错误geo.h:21:24:error:“vector”未在此范围内声明
。是否必须向每个向量声明添加std::如下:std::vector
@MattMcNabb@Dinosaur是的。如果您使用的是STL类型,并且没有使用命名空间的std;
在您的代码中,您需要将std:
放在该类型的开头。例如,std::map
,std::vector
,std::string
,std::cout
,std::endl
。是的,我已经更正了。但也有同样的问题。我在编译中没有任何错误,但在运行project输出时没有file.csv文件仍然为空:(.当我使用命名空间删除std时,我发现此错误geo.h:21:24:error:“vector”未在此范围内声明。是否必须向每个向量声明添加std::如下:std::vector
?是的,您必须通过在属于std>的类或函数前面添加std::来调整它。)espace当我使用命名空间删除std时,我发现了这个错误geo.h:21:24:error:“vector”未在此范围内声明。是否必须像这样向每个向量声明添加std::std::vector
?是的,您必须通过在属于std的类或函数前面添加std::来调整它命名空间…并从main.cpp
的源代码中删除#include“geo.cpp”
是的。但也有同样的问题。我在编译中没有任何错误,但在运行project时,输出文件file.csv仍然为空:(@WhozCraig编译成功,但我发现此错误分段错误(内核转储)
@恐龙无关问题。这意味着您的代码已损坏,但至少现在它已链接。现在您可以调试了=P@Dinosaur首先,我要确保用于快速排序的比较器建立了真正严格的弱排序,因为我现在可以看到它没有。其次,您有多个隐式转换double
到int
,这似乎是错误的,但嘿,这不是我的代码。祝你好运…并从的源代码中删除#包括“geo.cpp”
#include <vector>
#include <stack>
using namespace std;
#include "Point.h"
#ifndef GEO_H
#define GEO_H
double myRand(double min, double max);
void PCEngine(char *theFileName, int pointsNBR);
void getFarthestPoints(vector<Point> v, Point &p1, Point &p2);
vector<Point> getCloudPoint(char *fileName);
void loadPointCloud(char *fileName, Point p[]);
int getLineNbr(char *fileName);
double myRand(double min, double max);
Point nextToTop(stack<Point> &S);
int orientation(Point p, Point q, Point r);
int compare(const void *vp1, const void *vp2);
vector<Point> convexHull(Point Points[], int n);
#endif /* GEO_H */
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <fstream>
#include <sstream>
using namespace std;
#include "geo.h"
#include "Point.h"
Point p0 = NULL;
double myRand(double min, double max) {
return (double) (min + ((float) rand() / RAND_MAX * (max - min + 1.0)));
}
int getLineNbr(char *fileName) {
string line;
int nbr = 0;
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
++nbr;
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return 0;
}
void loadPointCloud(char *fileName, Point Points[]) {
string line;
string token;
double tab[3];
ifstream file(fileName);
if (file.is_open()) {
int lineNbr = -1;
while (getline(file, line)) {
++lineNbr;
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
++cpt;
}
Point p(tab[0], tab[1], tab[2]);
p.setColor(myRand(0, 255), myRand(0, 255), myRand(0, 255));
Points[lineNbr] = p;
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
}
vector<Point> getCloudPoint(char *fileName) {
string line;
string token;
vector<Point> v;
double tab[3];
// double x;
// double y;
// double z;
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
cpt++;
}
Point p(tab[0], tab[1], tab[2]);
p.setColor(myRand(0, 255), myRand(0, 255), myRand(0, 255));
v.push_back(p);
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return v;
}
/**
* This is an Engine to generate the points
* and to save their in the "file".csv
*
* @param theFileName
* @param pointsNBR points number
*/
void PCEngine(char *theFileName, int pointsNBR) {
cout << "PCEngine : Ok!\n";
ofstream file(theFileName);
if (file.is_open()) {
cout << "Ok!\n";
for (int i = 0; i < pointsNBR; i++) {
file << myRand(-1, 1) << "," << myRand(-1, 1) << "," << myRand(-1, 1) << "\n";
}
file.close();
} else {
cout << "Unable to open file\n";
}
}
void getFarthestPoints(vector<Point> v, Point &p1, Point &p2) {
if (v.size() < 2) {
cout << "The number of points is very small.\n";
exit(0);
} else {
int indexP1 = -1;
int indexP2 = -1;
double maxDist = 0;
double dist = 0;
for (int i = 0; i < v.size() - 1; ++i) {
for (int j = i + 1; j < v.size(); ++j) {
dist = v[i].dist3D(v[j]);
if (maxDist < dist) {
maxDist = dist;
indexP1 = i;
indexP2 = j;
}
}
}
p1 = v[indexP1];
p1.print(cout);
p2 = v[indexP2];
p2.print(cout);
}
}
/** An utility function to find next to top in a stack */
Point nextToTop(stack<Point> &S) {
Point p = S.top();
S.pop();
Point res = S.top();
S.push(p);
return res;
}
int orientation(Point p, Point q, Point r) {
int val = (q.getY() - p.getY()) * (r.getX() - q.getX()) -
(q.getX() - p.getX()) * (r.getY() - q.getY());
if (val == 0) return 0;
// colinear
return (val > 0) ? 1 : 2; // clock or counterclock wise
}
// A function used by library function qsort() to sort an array of
// Points with respect to the first Point
int compare(const void *vp1, const void *vp2) {
Point *p1 = (Point *) vp1;
Point *p2 = (Point *) vp2;
// Find orientation
int o = orientation(p0, *p1, *p2);
if (o == 0) {
//the distance square between p0 and p1
double distSquare1 = p0.dist2D(*p1) * p0.dist2D(*p1);
//the distance square between p0 and p2
double distSquare2 = p0.dist2D(*p2) * p0.dist2D(*p2);
return (distSquare2 >= distSquare1) ? -1 : 1;
}
return (o == 2) ? -1 : 1;
}
vector<Point> convexHull(Point Points[], int n) {
vector<Point> v;
// Find the bottommost Point
int ymin = Points[0].getY();
int min = 0;
for (int i = 1; i < n; i++) {
int y = Points[i].getY();
// Pick the bottom-most or chose the left most Point in case of tie
if ((y < ymin) || (ymin == y && Points[i].getX() < Points[min].getX()))
ymin = Points[i].getY(), min = i;
}
// Place the bottom-most Point at first position
Points[min] = Points[0].swap(Points[min]);
// Sort n-1 Points with respect to the first Point. A Point p1 comes
// before p2 in sorted ouput if p2 has larger polar angle (in
// counterclockwise direction) than p1
p0 = Points[0];
qsort(&Points[1], n - 1, sizeof (Point), compare);
// Create an empty stack and push first three Points to it.
stack<Point> S;
S.push(Points[0]);
S.push(Points[1]);
S.push(Points[2]);
// Process remaining n-3 Points
for (int i = 3; i < n; i++) {
// Keep removing top while the angle formed by Points next-to-top,
// top, and Points[i] makes a non-left turn
while (orientation(nextToTop(S), S.top(), Points[i]) != 2)
S.pop();
S.push(Points[i]);
}
// Now stack has the output Points, print contents of stack
while (!S.empty()) {
Point p = S.top();
cout << "(" << p.getX() << ", " << p.getY() << ")" << endl;
v.push_back(Point(p.getX(), p.getY(), 0));
S.pop();
}
return v;
}
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
#include "Point.h"
#include "geo.cpp"
int main(int argc, char** argv) {
cout << "main\n";
char *theFileName = "file.csv";
PCEngine(theFileName, 100);
return 0;
}
main.cpp: In function ‘int main(int, char**)’:
main.cpp:20:29: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/dgilog build/Debug/GNU-Linux-x86/Plan.o build/Debug/GNU-Linux-x86/Point.o build/Debug/GNU-Linux-x86/Straight.o build/Debug/GNU-Linux-x86/Vect.o build/Debug/GNU-Linux-x86/geo.o build/Debug/GNU-Linux-x86/main.o
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'p0'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'myRand(double, double)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'getLineNbr(char*)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'loadPointCloud(char*, Point*)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'getCloudPoint(char*)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'PCEngine(char*, int)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'getFarthestPoints(std::vector<Point, std::allocator<Point> >, Point&, Point&)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'nextToTop(std::stack<Point, std::deque<Point, std::allocator<Point> > >&)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'orientation(Point, Point, Point)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'compare(void const*, void const*)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
/usr/bin/ld: error: build/Debug/GNU-Linux-x86/main.o: multiple definition of 'convexHull(Point*, int)'
/usr/bin/ld: build/Debug/GNU-Linux-x86/geo.o: previous definition here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/dgilog] Error 1
make[2]: Leaving directory `/home/abouabdillehmsk/NetBeansProjects/DGILOG'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/abouabdillehmsk/NetBeansProjects/DGILOG'
make: *** [.build-impl] Error 2
#include "geo.cpp"
g++ main.cpp geo.cpp -o main.out
#ifndef POINT_H
#define POINT_H
#include <math.h>
#include <iostream>
//code
#endif