C++ C++;链接器错误iostream重载
我试图编译包含这两个文件的程序时,出现两个链接器错误(导致问题,尤其是粗体的行) 我是C++新手,请原谅我的无知。< /P> Assignment1.obj:错误LNK2001:未解析的外部符号“public:class Vector\uu thiscall Vector::operator^(class Vector)”(??TVector@@QAE?AV0@V0@@Z)C++ C++;链接器错误iostream重载,c++,overloading,iostream,linker-errors,C++,Overloading,Iostream,Linker Errors,我试图编译包含这两个文件的程序时,出现两个链接器错误(导致问题,尤其是粗体的行) 我是C++新手,请原谅我的无知。< /P> Assignment1.obj:错误LNK2001:未解析的外部符号“public:class Vector\uu thiscall Vector::operator^(class Vector)”(??TVector@@QAE?AV0@V0@@Z) 1> Assignment1.obj:错误LNK2001:未解析的外部符号“class std::basic_ostrea
1> Assignment1.obj:错误LNK2001:未解析的外部符号“class std::basic_ostream>和u cdecl操作符很难从给出的信息中看出问题所在;你在C++代码> C < /C>文件中有C++代码,但是如果你没有编译器来识别它,你的编译器会抱怨。为避免产生疑问,请重命名
point.c
文件point.cpp
一般来说,使用你想成为朋友的东西的转发声明是个好主意;这可以确保您与一些顶级函数而不是一些内部函数(特别是友元类)成为朋友
//转发声明
类点;
std::istream&operator>>(std::istream&Point&);
std::ostream&operator>(std::istream&Point&);
friend std::ostream&operator#包含
没有
向量类中有一个错误,没有定义“^”运算符。此外,出于优先考虑,最好不要在操作中使用^。除此之外,它真的不明显是什么
关于你的ostream问题,我不完全确定…第一个错误是抱怨缺少你作为朋友使用的向量
类定义,而不是向量
,它包含在你的.cpp
中,而不是资本化。你打算使用不同的课程吗
#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 );**
#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;
}
// 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>