不知道是哪里出了问题..找不到哪里出错了..
错误是:
error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream \> &,class Table const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Table@H@@@Z) referenced in function _main
代码如下:
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;
}
main.cpp 文件是:
#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;
}
我猜想 Table.h 文件中的 ostream
函数有问题。但我不知道如何解决。有人可以帮我吗?谢谢!
最佳答案
如果您在打开警告的情况下进行编译,您会得到一个提示。这是来自 gcc:
warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const Table&)’ declares a non-template function [-Wnon-template-friend]
note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
所以问题出在你的friend
上声明:
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);
一旦你解决了这个问题,你的流运算符(operator)就会出现其他问题。您正在尝试流式传输 vector<int>
没有定义函数。但这应该会让您走上正确的轨道。
关于c++ - 为什么我的输出 operator<<() 函数定义导致未解析的外部符号错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26682771/