 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
skylendar Guest
|
Posted: Sat Sep 04, 2004 1:25 am Post subject: Question about template and void function. |
|
|
Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
class Y
{
public:
void otherfunc() { ... }
}
main()
{
A<Y> a;
a.func();
}
Unfortunately, it doesn't compile (with g++ 3.4.1) otherfunc must be
declared beforehand.
How to do that ?
Thanx for the answer
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Radoslaw Grzanka Guest
|
Posted: Sat Sep 04, 2004 10:43 am Post subject: Re: Question about template and void function. |
|
|
Dnia Fri, 03 Sep 2004 21:25:39 -0400, skylendar napisa³(a):
| Quote: | Let's regard the following example:
|
This example is wrong. Let's see:
| Quote: |
template<class X> class A
{
public:
void func() { otherfunc(); }
|
What otherfunc()? Where is this otherfunc()? You mean in class Y? You have
to have object of class Y to call function from there or that function
must be declared static. In template you are not passing objects just
classes.
In your example you are trying to invoke method from class A (which
obviously does not exist or global scope function (which does not exist
either)
Semicolon, I presume.
| Quote: | class Y
{
public:
void otherfunc() { ... }
}
}
|
same here.
| Quote: | main()
{
A<Y> a;
a.func();
}
}
|
Not needed brace.
| Quote: | Unfortunately, it doesn't compile (with g++ 3.4.1) otherfunc must be
declared beforehand.
How to do that ?
Thanx for the answer
|
Try this code and see why your code did not compile.
template<class X> class A
{
public:
void func() { X::otherfunc(); }
};
class Y
{
public:
static void otherfunc() { }
};
main()
{
A<Y> a;
a.func();
}
or this:
template<class X> class A
{
X b;
public:
void func() { b.otherfunc(); }
};
class Y
{
public:
void otherfunc() { }
};
main()
{
A<Y> a;
a.func();
}
Or maybe.. just maybe you did want inheritance. This is not likely as you
are using templates. But here goes the code:
class Y
{
public:
void otherfunc() { }
};
template<class X> class A : public Y
{
public:
void func() { otherfunc(); }
};
main()
{
A<Y> a;
a.func();
}
HTH,
Cheers,
Radoslaw
--
"Oceniaj± mnie, choæ nic o mnie nie wiedz±. To dlatego jestem sam" - Shrek
============================================================================
e-mail: sad[na]rpg[kropka]pl JID: radekg[na]chrome[kropka]pl
L:C++ E+++ T-- !R P+++ D G++ F:ADoM RL--- !RLA W:CP Q++ AI++ RN+ Hp- Re++ S+
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebø Guest
|
Posted: Sat Sep 04, 2004 10:45 am Post subject: Re: Question about template and void function. |
|
|
"skylendar" <skylendar (AT) yahoo (DOT) com> wrote
| Quote: | Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
|
The otherfunc() called above isn't defined anywhere, perhaps you meant
something like:
template<class X> class A
{
public:
void func() { x.otherfunc(); }
private:
X x;
};
With this, it compiles and runs as expected.
| Quote: | class Y
{
public:
void otherfunc() { ... }
}
main()
{
A<Y> a;
a.func();
}
Unfortunately, it doesn't compile (with g++ 3.4.1) otherfunc must be
declared beforehand.
How to do that ?
|
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sun Sep 05, 2004 11:37 am Post subject: Re: Question about template and void function. |
|
|
skylendar wrote:
| Quote: | Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
class Y
{
public:
void otherfunc() { ... }
}
main()
|
You need to declare the return type. I'm sure your compiler warned
you about this at the very least.
| Quote: | {
A<Y> a;
a.func();
}
Unfortunately, it doesn't compile (with g++ 3.4.1) otherfunc must be
declared beforehand.
How to do that ?
|
This is a variant of a question answered in my template FAQ:
<http://womble.decadentplace.org.uk/cplusplus/template-faq.html#base-lookup>.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Sep 08, 2004 7:19 pm Post subject: Re: Question about template and void function. |
|
|
Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote
| Quote: | skylendar wrote:
Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
class Y
{
public:
void otherfunc() { ... }
}
main()
You need to declare the return type. I'm sure your compiler warned
you about this at the very least.
|
Actually, he did declare the return type. It's a class Y. Not that I
think that this is what he wanted.
At least as written, I don't think it is. In the template, otherfunc()
can only be a global function, regardless of how the template is
instantiated. And there is no such global function.
--
James Kanze GABI Software http://www.gabi-soft.fr
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Fri Sep 10, 2004 1:29 am Post subject: Re: Question about template and void function. |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote in message
news:<slrncjk4fc.83h.ben-public-nospam (AT) shadbolt (DOT) i.decadentplace.org.uk>...
skylendar wrote:
Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
class Y
{
public:
void otherfunc() { ... }
}
main()
You need to declare the return type. I'm sure your compiler warned
you about this at the very least.
Actually, he did declare the return type. It's a class Y. Not that I
think that this is what he wanted.
|
Indeed; C++ doesn't allow declaring return types in the
declaration of functions, so such errors are diagnosed
more clearly by good compilers.
-- James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Fri Sep 10, 2004 1:53 pm Post subject: Re: Question about template and void function. |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote:
| Quote: | Ben Hutchings <ben-public-nospam (AT) decadentplace (DOT) org.uk> wrote in message
news:<slrncjk4fc.83h.ben-public-nospam (AT) shadbolt (DOT) i.decadentplace.org.uk>...
skylendar wrote:
Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
class Y
{
public:
void otherfunc() { ... }
}
main()
You need to declare the return type. I'm sure your compiler warned
you about this at the very least.
Actually, he did declare the return type. It's a class Y. Not that I
think that this is what he wanted.
snip
This is a variant of a question answered in my template FAQ:
http://womble.decadentplace.org.uk/cplusplus/template-faq.html#base-lookup>.
At least as written, I don't think it is. In the template, otherfunc()
can only be a global function, regardless of how the template is
instantiated. And there is no such global function.
|
....and there are several other syntax errors. You're right; I've
commented on one omission but mentally filled-in others. The things I
filled in were:
- " : public X" after "class A"
- semi-colon after the closing brace of class template A
- semi-colon after the closing brace of class Y
Let's see if I guessed right. skylendar?
--
Ben Hutchings
It is easier to write an incorrect program than to understand a correct one.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
skylendar Guest
|
Posted: Mon Sep 27, 2004 10:27 am Post subject: Re: Question about template and void function. |
|
|
"Terje Slettebø" <tslettebo (AT) hotmail (DOT) com> wrote
| Quote: | "skylendar" <skylendar (AT) yahoo (DOT) com> wrote in message
news:7eb5ab14.0409021341.27c3b49d (AT) posting (DOT) google.com...
Let's regard the following example:
template<class X> class A
{
public:
void func() { otherfunc(); }
}
The otherfunc() called above isn't defined anywhere, perhaps you meant
something like:
template<class X> class A
{
public:
void func() { x.otherfunc(); }
private:
X x;
};
With this, it compiles and runs as expected.
class Y
{
public:
void otherfunc() { ... }
}
main()
{
A<Y> a;
a.func();
}
Unfortunately, it doesn't compile (with g++ 3.4.1) otherfunc must be
declared beforehand.
How to do that ?
|
Obviously, it was int main() with a closing brace... but the problem
is not there:
How to instruct the compiler that otherfunc() is a method from X and
not a global function ? This example worked with g++ 3.3.x but no
longer with gcc 3.4.x and its new hand made parser.
g++ refuses "void func() { X::otherfunc(); }" and asks for a
preliminary declaration of otherfunc()
THank you for the possible answer,
C.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|