Cannot compile with templated classes

S

sam.barker0

Hi all,

I have this following error

In test.h
===========================================
#include <iostream>
#include <cstddef>
#include <utility>

template<typename T> class myclass;
template<typename T> std::eek:stream& operator<< (std::eek:stream&, const
myclass<T>&);

template <typename T> class myclass {

public:
friend std::eek:stream& operator<< <T> (std::eek:stream& os, const
myclass<T>& var1);
....
...
...
private:
struct mysubclass
{

int par1;
int par2;
};
};
#include "myclass.tem"

=============================================
In myclass.tem,I have



===============================================
#include <cassert>
#include <algorithm>
template<typename T>
std::eek:stream& operator<< (std::eek:stream& values, const myclass<T>&
var1)
{
typename myclass<T>::mysubclass *current;
current->par1=89;
.................................
....................................
}


When I compile I get the error

In myclass.tem
error: ‘par1’ was not declared in this scope

What am I doing wrong.How can solve this.

Cheers,
Sam
 
B

Barry

Hi all,

I have this following error

In test.h
===========================================
#include <iostream>
#include <cstddef>
#include <utility>

template<typename T> class myclass;
template<typename T> std::eek:stream& operator<< (std::eek:stream&, const
myclass<T>&);

template <typename T> class myclass {

public:
 friend std::eek:stream& operator<< <T> (std::eek:stream& os, const
myclass<T>& var1);
...
..
..
private:
struct mysubclass
{

 int par1;
int par2;};
};

#include "myclass.tem"

=============================================
In myclass.tem,I have

===============================================
#include <cassert>
#include <algorithm>
template<typename T>
std::eek:stream& operator<< (std::eek:stream& values, const myclass<T>&
var1)
{
          typename myclass<T>::mysubclass *current;
          current->par1=89;
          .................................
          ....................................

}

When I compile I get the error

In myclass.tem
error: ‘par1’ was not declared in this scope

What am I doing wrong.How can solve this.


Well, I don't see any error with this code,
except a runtime error, you didn't initialize the pointer "current".

and VC2005, GCC4.3, Comeau Online compiles file.
 
J

James Kanze

I have this following error
In test.h
===========================================
#include <iostream>
#include <cstddef>
#include <utility>
template<typename T> class myclass;
template<typename T> std::eek:stream& operator<< (std::eek:stream&, const
myclass<T>&);
template <typename T> class myclass {
public:
friend std::eek:stream& operator<< <T> (std::eek:stream& os, const
myclass<T>& var1);
...
..
..
private:
struct mysubclass
{
int par1;
int par2;
};
};
#include "myclass.tem"
=============================================
In myclass.tem,I have
===============================================
#include <cassert>
#include <algorithm>
template<typename T>
std::eek:stream& operator<< (std::eek:stream& values, const myclass<T>&
var1)
{
typename myclass<T>::mysubclass *current;
current->par1=89;
.................................
....................................
}
When I compile I get the error
In myclass.tem
error: ?par1? was not declared in this scope
What am I doing wrong.How can solve this.

What complier are you using? What is the exact error message?
And the exact code which triggers it?

Deleting the "..." lines in your code and adding a return to the
operator<<, it compiles with all of the compilers available to
me (Sun CC, g++ and VC++), including if add code to instantiate
the function.
 
S

sam.barker0

Thanks for the help guys.
Yeah.The pointer is to be initialised.

The exact error message is
myclass.tem: In function ‘std::eek:stream& operator<<(std::eek:stream&,
const myclass<T>&)’:
myclass.tem:61: error: ‘par1’ was not declared in this scope
myclass.tem:64: error: ‘par2’ was not declared in this scope
cc1plus: warnings being treated as errors


I am trying to compile it in g++.

I am stumped.
 
J

James Kanze

The exact error message is
myclass.tem: In function ?std::eek:stream& operator<<(std::eek:stream&,
const myclass<T>&)?:
myclass.tem:61: error: ?par1? was not declared in this scope
myclass.tem:64: error: ?par2? was not declared in this scope
cc1plus: warnings being treated as errors
I am trying to compile it in g++.

With what code? The code you posted cannot compile, as such,
because of the .... lines. Deleting those, it *does* compile
with g++ (both versions 4.1.0 and 3.4.0). So please post an
exact example (copy/pasted) of the code which doesn't compile,
with the exact error message you get, and which version of the
compiler you are using.
 
S

sam.barker0

Thanks for you support James.Yes, you were correct.The problem was
with the way I was calling the classes.

Cheers,
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top