[英]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.