C++ 为什么我的输出运算符<&书信电报;()函数定义导致未解决的外部符号错误?
我不知道它怎么了。。我找不到错误在哪里 错误是: 错误LNK2019:未解析的外部符号“类” 标准::基本的_ostream>和u cdeclC++ 为什么我的输出运算符<&书信电报;()函数定义导致未解决的外部符号错误?,c++,visual-studio,lnk2019,C++,Visual Studio,Lnk2019,我不知道它怎么了。。我找不到错误在哪里 错误是: 错误LNK2019:未解析的外部符号“类” 标准::基本的_ostream>和u cdecl 运算符如果在启用警告的情况下编译,将得到提示。这是来自gcc的: 警告:好友声明'std::ostream&operator@self为什么-1代表虚空总管?@JessicaJin“为什么-1代表虚空总管?”?“因为编译器支持它,但标准定义拒绝了它:-P。。。但是你应该解决这个问题,是的。嗨,巴里,非常感谢你的帮助!但我有点困惑,什么时候应该放模板?我曾
运算符如果在启用警告的情况下编译,将得到提示。这是来自gcc的:
警告:好友声明'std::ostream&operator@self为什么-1代表虚空总管?@JessicaJin“为什么-1代表虚空总管?”?“因为编译器支持它,但标准定义拒绝了它:-P。。。但是你应该解决这个问题,是的。嗨,巴里,非常感谢你的帮助!但我有点困惑,什么时候应该放
模板?我曾经认为在类表
前面有一个模板
,并且朋友
声明在类表
的定义中,那么模板
是没有必要的…@JessicaJin请对您的问题再做一点澄清。
Table.h:
#include <cassert>
#include <vector>
#include <iostream>
#include <cstddef>
template <class T>
class Table {
protected:
std::vector<int>
dimensions;
// vector whose length is the number of table dimensions
// and whose elements give the size of each dimension
std::vector<int>
offsetArray;
std::vector<T>
data;
// vector of table values, ordered according
// to the offsets given in offsetArray
int
numCells;
// total number of cells in the interior of the table
public:
class iterator {
public:
iterator() : myTable(0), index(0) {}
iterator(Table * t, int i = 0)
{ myTable = t; index = i; };
// construct myself to point to index i
// of my containing Table object t
iterator & operator++();
// advance one element and return a reference to me
iterator operator++(int);
// advance one element and return my previous value
iterator & operator=(iterator i);
// set my table and position to be the same as j's
bool operator==(const iterator & i);
// return true if I'm positioned at the same element as i
bool operator!=(const iterator & i);
// return true if I'm positioned at a different
// element than i
T & operator*();
// return a reference to the element at my current position
int getIndex() { return index;}
// return my index value
private:
Table * myTable;
int index;
};
Table(){};
// Construct myself to be empty
Table(const std::vector<int> & v);
// Construct myself to be of the
// dimensionality given by v, with elements created
// by the default constructor
Table(const std::vector<int> & v, const T & initVal);
// Construct myself to be of the
// dimensionality given by v, with all cells
// initialized to initVal
T & operator[](const std::vector<int>);
// return a reference to the cell
// value indexed by v
const T & operator[](const std::vector<int>) const;
// return a const reference to the cell
// value indexed by v
bool operator==(const Table<T> & t);
// Return true if I'm the same dimensionality
// as t and hold the same values
std::vector<int> size() const { return dimensions; }
// Return my dimension vector
std::vector<int> locationFromIndex(int index) const;
// return a location vector corresponding to
// an index into the (linear) data vector
int getNumCells() {return numCells};
// return the number of cells I contain
iterator begin() {return iterator(this); };
iterator end() {return iterator(this, numCells); };
friend std::ostream & operator<<(std::ostream & out, const Table<T> & a);
};
template<class T>
std::ostream & operator<<(std::ostream & out, const Table<T> & a){
for(size_t i=0;i<a.data.size();i++){
out<<a.locationFromIndex(i)<<a.data[i];
}
return out;
}
#include <iostream>
#include <cstdlib>
#include "Table.h"
using namespace std;
void main() {
// make this a 4x3x2 matrix, initialized with 1's
vector<int> v;
v.push_back(4);
v.push_back(3);
v.push_back(2);
Table<int> t(v, 1);
// print it
cout << t << endl;
}
friend std::ostream & operator<<(std::ostream & out, const Table<T> & a);
template <typename U>
friend std::ostream & operator<<(std::ostream & out, const Table<U> & a);