I have a pretty simple template which is a container which is an array of T. I am getting a syntax error :
container.h(7): error C2143: syntax error : missing ';' before '&'. I have tried removing the declaration there but then the error just skips over to the definition. Would appreciate any help.
EDIT: now i fixed the using namespace thing, but another error popped:
container.h(8): error C2975: 'Container' : invalid template argument for 'unnamed-parameter', expected compile-time constant expression
#include <typeinfo.h>
#include <assert.h>
#include <iostream>
#pragma once
using namespace std;
template <typename T, int> class Container;
template <typename T, int> ostream& operator<< <>(ostream &, const Container<T,int> &);
template<class T , int capacity=0> class Container
{
//using namespace std;
private:
T inside[capacity];
public:
Container()
{
}
~Container(void)
{
}
void set(const T &tType, int index)
{
assert(index>=0 && index<= capacity);
inside[index] = tType;
}
T& operator[](int index)
{
assert(index>=0 && index<= capacity);
return inside[index];
}
friend ostream& operator<< <>(ostream& out, const Container<T,int> c);
{
for(int i=0;i<sizeof(inside)/sizeof(T);i++)
out<<c.inside[i]<< "\t";
return out;
}
};
You probably want:
template <typename T, int N>
ostream& operator<<(ostream &, const Container<T,N> &);
// ^ here you need N, not int!
or, since you don't actually need the forward declaration, you can simply use this implementation in your class:
friend ostream& operator<<(ostream & out, const Container<T,capacity>& c)
{
for(int i=0;i<capacity;++i)
out<<c.inside[i]<< "\t";
return out;
}
You want something like:
template <typename T, int N>
friend ostream& operator<<(ostream & out, const Container<T,N>& c) {
for(int i=0;i<sizeof(inside)/sizeof(T);i++)
out<<c.inside[i]<< "\t";
return out;
}
Related
I need to inherit all functions from std::vector and I want to overload operators to make a complete matrix class.
There is not too much documentation on this topic.
Matriz.h
#include <vector>
#include <iostream>
using namespace std;
template<typename T>
class Matriz:vector<T>
{
public:
using vector<T>::vector;
private:
}
Matriz.cpp
int main()
{
Matriz<int> dani;
dani.push_back(2); //Here is the error and I don`t know what it happens
}
When I want to initialize it, I got an error.
Severity Code Description Project File Line Suppression State
Error C2247 'std::vector<int,std::allocator<_Ty>>::push_back' not accessible because 'Matriz<int>' uses 'private' to inherit from 'std::vector<int,std::allocator<_Ty>>'
This should work:
#include <vector>
#include <iostream>
template<typename T>
class Matriz: public std::vector<T>
{
public:
using std::vector<T>::vector;
private:
};
int main()
{
Matriz<int> dani;
dani.push_back(2);
dani.push_back(3);
for(const auto& it: dani)
std::cout << it << " ";
}
Inherit from vector class is a mistake
It is very difficult and produces a lot of errors as it is said here.
I have made a class which has a vector and inlcudes:
template
operator overloading
matrix operations
LINK : vector.h
#pragma once
#include <vector>
#include <iostream>
template<class T>
class Vector
{
public:
Vector();
Vector(int);
Vector(int, int);
~Vector();
std::vector<std::vector<T>> v;
bool Check_Size() ;
template<typename T1> bool Check_Size(std::vector<T1>&) ;
template<typename T1> bool Check_Size_Fast(std::vector<T1>&);
void Print();
void Transponse();
void Inverse();
void Inverse2();
void Inverse3();
template<class T,class Q>
friend std::vector<std::vector<T>> operator* (const Q , Vector<T> );
template<class T,class Q>
friend std::vector<std::vector<T>> operator* (Vector<T> , const Q );
template<class T>
friend std::vector<std::vector<T>> operator*(Vector<T>& , Vector<T>&);
template<typename T>
friend std::vector<std::vector<T>> operator+(Vector<T> &, Vector<T> &);
template<typename T>
friend std::vector<std::vector<T>> operator-(Vector<T> &, Vector<T> &);
Vector<T>& operator = (const std::vector<std::vector<T>>& v)
{
this->v = v;
return *this;
}
std::vector<std::vector<T>>& operator +=( Vector<T>&v) {
return v + (*this);
}
std::vector<std::vector<T>>& operator -=(Vector<T>&v) {
return v - (*this);
}
std::vector<std::vector<T>>& operator *=(Vector<T>&v) {
return v * (*this);
}
private:
void Recursive_Check(std::vector<T>&);
};
I am working on a template Graph data structure, which is an STL vector of GraphNode objects. I defined the GraphNode class nested inside the Graph class and when I invoke the overloaded insertion operator for the GraphNode object inside the overloaded insertion operator for the Graph object Visual Studio 15 (C++) reports,
(30): warning C4346: 'myGraph<T>::myGraphNode': dependent name is not a type
(30): note: prefix with 'typename' to indicate a type
(30): error C2061: syntax error: identifier 'myGraphNode'
(33): error C2805: binary 'operator <<' has too few parameters
template <typename T>
ostream& operator<<(ostream& strm, const myGraph<T>::myGraphNode& gn)
adding the word typename to the second formal parameter
template <typename T>
ostream& operator<<(ostream& strm, typename const myGraph<T>::myGraphNode& gn)
The compiler generates the following error
(49): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'const myGraph<int>::myGraphNode' (or there is no acceptable conversion)
I get the same error if I typename const .... or const typename ...
For completeness here is all the code somewhat simplified for this post.
Thanks for any help
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef unsigned int uint;
template <typename T>
class myGraph {
public:
class myGraphNode {
public:
myGraphNode(T val = T());
T mData;
}; // end class myGraphNode
myGraph();
uint addGraphNode(T data);
vector<myGraphNode> mGraphNodes;
}; // end class myGraph
// myGraphNode
template <typename T>
myGraph<T>::myGraphNode::myGraphNode(T val) : mData(val) {}
template <typename T>
ostream& operator<<(ostream& strm, typename const myGraph<T>::myGraphNode& gn) {
strm << gn.mData << std::endl;
return strm;
}
// myGraph
template <typename T>
myGraph<T>::myGraph() {}
template <typename T>
uint myGraph<T>::addGraphNode(T data) {
myGraph<T>::myGraphNode node(data);
mGraphNodes.push_back(node);
}
template <typename T>
ostream& operator<<(ostream& strm, const myGraph<T>& g) {
for (uint i = 0; i < g.mGraphNodes.size(); ++i)
cout << g.mGraphNodes[i] << endl;
return strm;
} // end operator<<(...)
int main()
{
myGraph<int> g;
g.addGraphNode(3);
g.addGraphNode(5);
cout << g << endl;
return 0;
}
Firstly, the correct syntax for parameter declaration should be
template <typename T>
ostream& operator<<(ostream& strm, const typename myGraph<T>::myGraphNode& gn)
// ~~~~~ ~~~~~~~~
Refer here for more informations.
Secondly, with the above declaration, when trying to call it in operator<< for myGraph<T> like cout << g.mGraphNodes[i] << endl;, T can't be deduced because of non-deduced contexts):
The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:
That means, you have to specify the template argument explicitly for it, e.g.
operator<<<T>(strm, g.mGraphNodes[i]);
// ~~~
But it's ugly. For your case, you can just implement operator<< for myGraph<T> like
template <typename T>
ostream& operator<<(ostream& strm, const myGraph<T>& g) {
for (uint i = 0; i < g.mGraphNodes.size(); ++i)
cout << g.mGraphNodes[i].mData << endl;
return strm;
}
BTW: You should give a return value for myGraph<T>::addGraphNode.
Template type deduction only matches patterns. It doesn't invert dependent types, because that (in the general case) is impossible.
The way to solve this problem is a technique I call Koenig operators.
friend std::ostream& operator<<(std::ostream& strm, const myGraphNode& gn) {
strm << gn.mData << std::endl;
return strm;
}
Put this in the body of myGraphNode.
class myGraphNode {
public:
myGraphNode(T val = T());
T mData;
friend std::ostream& operator<<(std::ostream& strm, const myGraphNode& gn) {
strm << gn.mData << std::endl;
return strm;
}
}; // end class myGraphNode
This is a non-template operator injected into surrounding namespace (only) reachable via ADL. Which is fancy words for "it just works".
I thought friend functions had access to all members. Even in this question it worked:
C++ friend function can't access private members
The answer given in that question seems identical to my code, and his compiled fine while mine just says array_ is pivate. Anyone know why?
.h:
#ifndef matrix_h
#define matrix_h
#include <iostream>
using namespace std;
template <typename Comparable>
class matrix
{
private:
size_t num_cols_;
size_t num_rows_;
Comparable **array_;
public:
friend ostream& operator<< (ostream& o, const matrix<Comparable> & rhs);
size_t NumRows();
size_t NumCols();
};
#endif
.cpp:
#include <iostream>
#include "matrix.h"
using namespace std;
template <typename Comparable>
ostream& operator<< (ostream& o, matrix<Comparable> & rhs){
size_t c = rhs.NumRows();
size_t d = rhs.NumCols();
for (int i = 0; i < c; i++){
for (int j = 0; j < d; j++){
o << rhs.array_[i][j]; //not allowed
}
o << endl;
}
return o;
}
template <typename Comparable>
size_t matrix<Comparable>::NumRows(){
return num_rows_;
}
template <typename Comparable>
size_t matrix<Comparable>::NumCols(){
return num_cols_;
}
int main(){
matrix<int> a;
cout << a << endl;
}
Say you use const in both places and add const to the declarations of numRows and numCols too. Then what's the problem? Well...
You think it's identical, but your code has a template. And the friend declaration
friend ostream& operator<< (ostream& o, const matrix<Comparable> & rhs);
is not a template, so it doesn't match the definition
template <typename Comparable>
ostream& operator<< (ostream& o, matrix<Comparable> & rhs){ // ...
which is a template. In fact gcc will give a warning:
matrix.h:16:79: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const matrix<Comparable>&)’ declares a non-template function [-Wnon-template-friend]
friend ostream& operator<< (ostream& o, const matrix<Comparable> & rhs);
^
matrix.h:16:79: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
It's tempting to just friend all specializations, like this:
template <typename T>
friend ostream& operator<< (ostream& o, const matrix<T> & rhs);
Unfortunately, this won't work for the reasons explained here: Why can templates only be implemented in the header file? You'll be able to compile matrix.cpp, but not a separate driver program, like this:
#include <iostream>
#include "matrix.h"
using namespace std;
int main() {
matrix<int> m;
cout << m << endl;
}
You get an undefined reference error. Instead, you really should just define your entire matrix class in the header and ditch the .cpp file.
It should be pointed out that this still has a problem: you can call this operator<< just fine, but you can't, say, take its address, because it can only be found by argument-dependent lookup.
auto ptr = static_cast<ostream&(*)(ostream&, const matrix<int>&)>(operator<<); // error
For it to be found by unqualified lookup, it must have a matching declaration at namespace scope. And it is actually impossible to write such a declaration (the syntax of C++ doesn't have any way to do it)! To fix this, we need to turn operator<< into a function template, defined inline:
template <typename Comparable>
class matrix {
// ...
template <typename T>
friend ostream& operator<<(ostream& o, const matrix<T>& rhs) {
// ...
}
// ...
};
// namespace-scope declaration
template <typename T>
ostream& operator<<(ostream& o, const matrix<T>& rhs);
Now the above code taking the address of the operator will work.
The compiler complains because the function you implement is different with the one you declare(in declaration rhs is decorated by const, in implementation it isn't).
After you add const in implementation, the compiler complains "Undefined reference" because the declaration and the implementation is still not the same. The implementation is a template function, the decalartion is not.
#ifndef matrix_h
#define matrix_h
#include <iostream>
using namespace std;
template <typename Comparable>
class matrix
{
private:
size_t num_cols_;
size_t num_rows_;
Comparable **array_;
public:
template<typename T>
friend ostream& operator<< (ostream& o, const matrix<T> & rhs);
size_t NumRows() const;
size_t NumCols() const;
};
template <typename Comparable>
ostream& operator<< (ostream& o, const matrix<Comparable> & rhs){
size_t c = rhs.NumRows();
size_t d = rhs.NumCols();
for (int i = 0; i < c; i++){
for (int j = 0; j < d; j++){
o << rhs.array_[i][j]; //not allowed
}
o << endl;
}
return o;
}
template <typename Comparable>
size_t matrix<Comparable>::NumRows() const{
return num_rows_;
}
template <typename Comparable>
size_t matrix<Comparable>::NumCols() const{
return num_cols_;
}
#endif
Ok I have updated the code:
#ifndef VECTOR_H
#define VECTOR_H
#include<cstdlib>
#include<vector>
#include<iostream>
using namespace std;
template < typename T>
class MyClass
{
public:
MyClass() : size(10), index(0) { vec = new T[10]; };
MyClass(int i) : size(i), index(0) { vec = new T[i]; };
friend bool Add(T i);
virtual ~MyClass();
friend ostream& operator<<(ostream& out, const T& obj);
private:
T * vec;
int size;
int index;
};
template <typename T>
virtual MyClass<T>::~MyClass()
{
delete[] vec;
}
template <typename T>
ostream& operator<<(ostream& out, const MyClass<T>& obj){
for (int i = 0; i < index; ++i)
out << obj[i] << " ";
out << endl;
}
template <typename T>
bool MyClass<T>::Add(T i){
if (size == index){
size *= 2;
realloc(vec, size);
}
vec[index++] = i;
}
#endif // !VECTOR_H
Error list:
Error 1 error C2039: 'add' : is not a member of 'MyClass' c:\users\mihaibogdan\documents\visual studio 2013\projects\dashit\dashit\header.h 41
You should use qualified names
friend std::ostream& operator<<( std::ostream& out, const T& obj);
Standard C++ names are declared in name space std.
Otherwise the compiler looks up unqualified name ostream in the global name space where it is not of course declared.
I have a template class called "KeyedCollection" that contains functions to insert data into a vector, as well as stream out the data. The vector is a private member function. I can't seem to figure out how to use the information from this vector in my overloading ostream friend function. Note: I cannot change the general structure of the class and the function arguments, they have to stay as they are. I list all of the class for reference, but the function in question is the last one.
#include "stdafx.h"
#include <iostream>
#include <vector>
#include "Windows.h"
#include <string>
using namespace std;
template <class K, class T>
class KeyedCollection {
public:
// Create an empty collection
KeyedCollection();
// Return the number of objects in the collection
int size() const;
// Insert object of type T with a key of type K into the
// collection using an “ignore duplicates” policy
void insert(const K&, const T&);
// Output data value of objects in the collection,
// one data value per line
friend ostream& operator<<(ostream&,
const KeyedCollection&);
private:
// Insert required members here
int objSize;
vector<T> objects;
};
template<class K, class T>
KeyedCollection<K,T>::KeyedCollection() {
objSize = 0;
vector<T> objects;
}
template<class K, class T>
int KeyedCollection<K,T>::size() const {
objSize = objects.size();
return objSize;
}
template<class K, class T>
void KeyedCollection<K,T>::insert(const K&,const T& c) {
objects.push_back(c);
}
// !!! function i am trying to define !!!
template<class K, class T>
ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {
outstream<<inst<<endl;
return outstream;
}
Also, I'm getting an error that says
"fatal error LNK1120: 1 unresolved externals"
and one that says
"error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class KeyedCollection const &)" (??6#YAAAV?$basic_ostream#DU?$char_traits#D#std###std##AAV01#ABV?$KeyedCollection#HVCustomer#####Z) referenced in function _main" ...
Just as a side question, any idea what those could be?
cppreference and Johannes Schaub - litb both provide the same method for getting this to work.
You want to make one single instance (called "specialization" in
generic terms) of that template a friend. You do it the following way
[...]
First make a forward declaration before the definition of your class:
template <class K, class T> class KeyedCollection;
template<class K, class T>
ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst);
Because the compiler knows from the parameter list that the template
arguments are T and U, you don't have to put those between <...>, so
they can be left empty.
Then make your friend declaration and be sure to add <> after operator<<:
template <class K, class T>
class KeyedCollection {
public:
// snip
friend ostream& operator<< <> (ostream& outstream,const KeyedCollection<K,T>& inst);
// snip
};
Finally you can define it:
template<class K, class T>
ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {
// Just an example
for (const auto& t : inst.objects)
{
std::cout << t << std::endl;
}
return outstream;
}
Live Example
Alternately, do what Yakk suggested.
template <class K, class T>
class KeyedCollection {
public:
// snip
friend ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {
for (const auto& t : inst.objects)
{
std::cout << t << std::endl;
}
return outstream;
}
// snip
};
Live Example