[英]C++ Linker error iostream overloading
尝试编译包含这两个文件的程序时出现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;
}
..... ..... .....}
从给出的信息很难看出问题所在; 您在.c
文件中包含C ++代码,但是如果您没有意识到这一点,则可能是编译器会抱怨。 为避免疑问,请重命名point.c
文件point.cpp
。
通常,对要成为朋友的事物使用前向声明是一个好主意; 这样可以确保您正在与某些顶级函数而不是某些内部函数成为朋友(尤其是针对朋友类)。
// forward declarations
class Point;
std::istream& operator>> ( std::istream&, Point&);
std::ostream& operator<< ( std::ostream&, Point );
// point class
class Point {
...
friend std::istream& operator>> ( std::istream&, Point&);
friend std::ostream& operator<< ( std::ostream&, Point );
...
#include <iostream>
没有.h。
向量类中的错误是未定义“ ^”运算符。 同样出于优先考虑,最好不要使用^之类的操作。 除其他外,它真的不很明显是什么...
不过,我不确定您的Ostream问题...
第一个错误是抱怨缺少作为您的朋友使用的Vector
类定义,而不是您包含在.cpp
vector
而不是大写。 您打算使用其他类吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.