[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Cannot compile with templated classes

sam.barker0

10/9/2008 6:09:00 AM

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::ostream& operator<< (std::ostream&, const
myclass<T>&);

template <typename T> class myclass {

public:
friend std::ostream& operator<< <T> (std::ostream& 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::ostream& operator<< (std::ostream& 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
5 Answers

Barry

10/9/2008 7:08:00 AM

0

On Oct 9, 2:09 pm, sam.bark...@gmail.com wrote:
> 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::ostream& operator<< (std::ostream&, const
> myclass<T>&);
>
> template <typename T> class myclass {
>
> public:
>  friend std::ostream& operator<< <T> (std::ostream& 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::ostream& operator<< (std::ostream& 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.

--
Best Regards
Barry

James Kanze

10/9/2008 8:18:00 AM

0

On Oct 9, 8:09 am, sam.bark...@gmail.com wrote:
> I have this following error

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

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

> template <typename T> class myclass {

> public:
> friend std::ostream& operator<< <T> (std::ostream& 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::ostream& operator<< (std::ostream& 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

sam.barker0

10/9/2008 12:08:00 PM

0

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

The exact error message is
myclass.tem: In function ‘std::ostream& operator<<(std::ostream&,
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.

James Kanze

10/10/2008 8:15:00 AM

0

On Oct 9, 2:08 pm, sam.bark...@gmail.com wrote:

> The exact error message is
> myclass.tem: In function ?std::ostream& operator<<(std::ostream&,
> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

sam.barker0

10/12/2008 6:55:00 AM

0

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

Cheers,
Sam
On Oct 10, 7:14 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Oct 9, 2:08 pm, sam.bark...@gmail.com wrote:
>
> > The exact error message is
> > myclass.tem: In function ?std::ostream& operator<<(std::ostream&,
> > 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.
>
> --
> James Kanze (GABI Software) email:james.ka...@gmail.com
> Conseils en informatique orientée objet/
> Beratung in objektorientierter Datenverarbeitung
> 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34