C++链接器错误 iostream 过载
在尝试编译包含这两个文件的程序时,我遇到了 2 个链接器错误(导致问题,特别是粗体行)
,而且我是 C++ 新手,请原谅我的无知。
Assignment1.obj:错误LNK2001:无法解析的外部符号“public:class Vector __thiscall Vector::operator^(class Vector)”(??TVector@@QAE?AV0@V0@@Z)
1>Assignment1.obj:错误LNK2001:无法解析的外部符号“class std::basic_ostream > & __cdecl 运算符<<(class std::basic_ostream > &,class Point)”(??6@YAAAV?$basic_ostream@DU?$char_traits@D@ std@@@std@@AAV01@VPoint@@@Z)
1>c:......\visual studio 2010\Projects\Assignment1\Release\Assignment1.exe : 致命错误 LNK1120: 2 个无法解析的外部
point.h 文件:
#ifndef SS_Point_H
#define SS_Point_H
#include "common.h"
//==================================================================
// Point Class Definition
//==================================================================
class Point {
friend class Vector;
protected:
int dimn; // # coords (1, 2, or 3 max here)
Error err; // error indicator
public:
double x, y, z; // z=0 for 2D, y=z=0 for 1D
//----------------------------------------------------------
// Lots of Constructors (add more as needed)
Point() { dimn=3; x=y=z=0; err=Enot; }
// 1D Point
Point( int a) {
dimn=1; x=a; y=z=0; err=Enot; }
Point( double a) {
dimn=1; x=a; y=z=0; err=Enot; }
// 2D Point
Point( int a, int b) {
dimn=2; x=a; y=b; z=0; err=Enot; }
Point( double a, double b) {
dimn=2; x=a; y=b; z=0; err=Enot; }
// 3D Point
Point( int a, int b, int c) {
dimn=3; x=a; y=b; z=c; err=Enot; }
Point( double a, double b, double c) {
dimn=3; x=a; y=b; z=c; err=Enot; }
// n-dim Point
Point( int n, int a[]);
Point( int n, double a[]);
// Destructor
~Point() {};
//----------------------------------------------------------
// Input/Output streams
**friend std::istream& operator>> ( std::istream&, Point&);
friend std::ostream& operator<< ( std::ostream&, Point );**
....}
而 point.c 文件:
#include "point.h"
#include "vector.h"
#include <iostream.h>
//==================================================================
// Point Class Methods
//==================================================================
//------------------------------------------------------------------
// Constructors (add more as needed)
//------------------------------------------------------------------
// n-dim Point
Point::Point( int n, int a[]) {
x = y = z = 0;
err = Enot;
switch (dimn = n) {
case 3: z = a[2];
case 2: y = a[1];
case 1: x = a[0];
break;
default:
err=Edim;
}
}
Point::Point( int n, double a[]) {
x = y = z = 0.0;
err = Enot;
switch (dimn = n) {
case 3: z = a[2];
case 2: y = a[1];
case 1: x = a[0];
break;
default:
err=Edim;
}
}
//------------------------------------------------------------------
// IO streams
//------------------------------------------------------------------
// Read input Point format: "(%f)", "(%f, %f)", or "(%f, %f, %f)"
**std::istream& operator>>( std::istream& input, Point& P) {**
char c;
input >> c; // skip '('
input >> P.x;
input >> c;
if (c == ')') {
P.setdim(1); // 1D coord
return input;
}
// else // skip ','
input >> P.y;
input >> c;
if (c == ')') {
P.setdim(2); // 2D coord
return input;
}
// else // skip ','
input >> P.z;
P.setdim(3); // 3D coord
input >> c; // skip ')'
return input;
}
// Write output Point in format: "(%f)", "(%f, %f)", or "(%f, %f, %f)"
**std::ostream& operator<<( std::ostream& output, Point P) {**
switch (P.dim()) {
case 1:
output << "(" << P.x << ")";
break;
case 2:
output << "(" << P.x << ", " << P.y << ")";
break;
case 3:
output << "(" << P.x << ", " << P.y << ", " << P.z << ")";
break;
default:
output << "Error: P.dim = " << P.dim();
}
return output;
}
..... …… …… }
i get 2 linker errors when trying to compile my program which includes these two files (causing the problem, in particular the lines in bold)
and i'm new to C++ so excuse my ignorance.
Assignment1.obj : error LNK2001: unresolved external symbol "public: class Vector __thiscall Vector::operator^(class Vector)" (??TVector@@QAE?AV0@V0@@Z)
1>Assignment1.obj : error LNK2001: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Point)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@VPoint@@@Z)
1>c:......\visual studio 2010\Projects\Assignment1\Release\Assignment1.exe : fatal error LNK1120: 2 unresolved externals
point.h file:
#ifndef SS_Point_H
#define SS_Point_H
#include "common.h"
//==================================================================
// Point Class Definition
//==================================================================
class Point {
friend class Vector;
protected:
int dimn; // # coords (1, 2, or 3 max here)
Error err; // error indicator
public:
double x, y, z; // z=0 for 2D, y=z=0 for 1D
//----------------------------------------------------------
// Lots of Constructors (add more as needed)
Point() { dimn=3; x=y=z=0; err=Enot; }
// 1D Point
Point( int a) {
dimn=1; x=a; y=z=0; err=Enot; }
Point( double a) {
dimn=1; x=a; y=z=0; err=Enot; }
// 2D Point
Point( int a, int b) {
dimn=2; x=a; y=b; z=0; err=Enot; }
Point( double a, double b) {
dimn=2; x=a; y=b; z=0; err=Enot; }
// 3D Point
Point( int a, int b, int c) {
dimn=3; x=a; y=b; z=c; err=Enot; }
Point( double a, double b, double c) {
dimn=3; x=a; y=b; z=c; err=Enot; }
// n-dim Point
Point( int n, int a[]);
Point( int n, double a[]);
// Destructor
~Point() {};
//----------------------------------------------------------
// Input/Output streams
**friend std::istream& operator>> ( std::istream&, Point&);
friend std::ostream& operator<< ( std::ostream&, Point );**
....}
while point.c file:
#include "point.h"
#include "vector.h"
#include <iostream.h>
//==================================================================
// Point Class Methods
//==================================================================
//------------------------------------------------------------------
// Constructors (add more as needed)
//------------------------------------------------------------------
// n-dim Point
Point::Point( int n, int a[]) {
x = y = z = 0;
err = Enot;
switch (dimn = n) {
case 3: z = a[2];
case 2: y = a[1];
case 1: x = a[0];
break;
default:
err=Edim;
}
}
Point::Point( int n, double a[]) {
x = y = z = 0.0;
err = Enot;
switch (dimn = n) {
case 3: z = a[2];
case 2: y = a[1];
case 1: x = a[0];
break;
default:
err=Edim;
}
}
//------------------------------------------------------------------
// IO streams
//------------------------------------------------------------------
// Read input Point format: "(%f)", "(%f, %f)", or "(%f, %f, %f)"
**std::istream& operator>>( std::istream& input, Point& P) {**
char c;
input >> c; // skip '('
input >> P.x;
input >> c;
if (c == ')') {
P.setdim(1); // 1D coord
return input;
}
// else // skip ','
input >> P.y;
input >> c;
if (c == ')') {
P.setdim(2); // 2D coord
return input;
}
// else // skip ','
input >> P.z;
P.setdim(3); // 3D coord
input >> c; // skip ')'
return input;
}
// Write output Point in format: "(%f)", "(%f, %f)", or "(%f, %f, %f)"
**std::ostream& operator<<( std::ostream& output, Point P) {**
switch (P.dim()) {
case 1:
output << "(" << P.x << ")";
break;
case 2:
output << "(" << P.x << ", " << P.y << ")";
break;
case 3:
output << "(" << P.x << ", " << P.y << ", " << P.z << ")";
break;
default:
output << "Error: P.dim = " << P.dim();
}
return output;
}
.....
.....
.....
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从给出的信息很难看出问题所在;您的
.c
文件中有 C++ 代码,但如果您的编译器无法识别它,它可能会抱怨。为避免产生疑问,请将您的point.c
文件重命名为point.cpp
。一般来说,使用您想要成为朋友的事物的前向声明是一个好主意;这确保您与一些顶级函数而不是一些内部函数(特别是关于友元类)成为朋友。
Its hard from the information given to see the problem; you have C++ code in a
.c
file, but presumably your compiler would complain if you hadn't got it to recognize that. To avoid doubt, rename yourpoint.c
filepoint.cpp
.Generally, its a good idea to use forward declarations of things you want to be friends; this ensures you are befriending some top-level function and not some inner function (particularly regards to friend classes).
没有.h。
您的向量类中存在错误,因为未定义“^”运算符。另外,出于优先级的原因,最好不要使用 ^ 之类的东西进行操作。除其他事项外,它确实不明显它是什么......
不过我不完全确定你的 ostream 问题......
There is no .h.
You have an error in your vector class in that the "^" operator is not defined. Also for reasons of precedence its really best not to use things like ^ for an operation. Amongst other things its really not obvious what it is ...
I'm not entirely sure on your ostream issue though ...
第一个错误是抱怨缺少您作为朋友使用的
Vector
类定义,而不是您已包含在中的
不是大小写。您打算使用不同的类吗?vector
。 cppThe first error is complaining about the lack of a
Vector
class definition which you're using as a friend, rather thanvector
which you've included in your.cpp
Not the capitalisation. Did you intend to use different classes?