I don't know what's wrong with it.. I can't find where the error is..
The error is:
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
The code is as followed:
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;
}
The main.cpp file is:
#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;
}
I guess there is something wrong with the ostream
function in the Table.h file. But I do not know how to fix it.. Can someone help me? Thanks!
If you compile with with warnings turned on, you'll get a hint. This is from 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)
So the problem is with your friend
declaration:
friend std::ostream & operator<<(std::ostream & out, const Table<T> & a);
It should be:
template <typename U>
friend std::ostream & operator<<(std::ostream & out, const Table<U> & a);
Once you fix that, there's other problems in your stream operator. You're trying to stream a vector<int>
for which there's no function defined. But this should put you on the right track.