Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Wyrframe

Templated function hassles

This topic is 6084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  
template <class T> int DoProgram (int argc, char* argv[]) { ... }
  
I use a templated class in this function. When compiling, it tells me that the above declaration line is "terminated incorrectly". Any chance you can tell me why? The reason I need to do this is because the compiler won't let me template the main() function. Somewhat understandable. [edit: added source tags] Edited by - Magmai Kai Holmlor on February 18, 2002 9:16:45 PM

Share this post


Link to post
Share on other sites
Advertisement
I think your code example has been munged by the php thingy.

trying putting it in square brackets with source and /source

I notice that your function does not parameterize on anything - so why do you need it to be a template?

Unless you instantiate something inside DoProgram based upon a template parameter.



Edited by - SteveC on February 18, 2002 2:53:50 PM

Share this post


Link to post
Share on other sites
His code looks like this:
    
template <class T> int DoProgram (int argc, char* argv[]) { ... }

If you're using MSVC I think you're forced to use (in this case) T as the return value or one of the parameters. If you're not using MSVC I'm not sure what the problem is.



Edited by - Null and Void on February 18, 2002 2:57:48 PM

Share this post


Link to post
Share on other sites

I''m using Borland''s BCC32 compiler (v5.5). I''ve been messing around with this, and still can''t get it to work quite right...

Continued efforts to help are highly appreciated!

Share this post


Link to post
Share on other sites
when you use templates, you can think of them as a large pool of functions. when you call such a function, the compiler cooses the right one for the args/return values you passed.. but when you don''t use the template class for args/return, the compiler does not know wich func it should coose...result: ERROR. This ist not a bad thing, as using templates makes no sense when you don''t pass a a templated arg/retval..

Share this post


Link to post
Share on other sites
  
#define STAMP template <class T>

int main(int argc, char* argv[]);
STAMP int DoProgram (int argc, char* argv[]); ;

int main(int argc, char* argv[]) {
return DoProgram(argc, argv);
}

STAMP int DoProgram (int argc, char* argv[]) {
// I need to make this a templated function in

// order to use the templated class below...


// My class...

ZArray *ca; char itype[16];

// Put either "int", "char", or "float" in itype[], and create the var based on that.


if ( *itype == 'i' ) ca = new ZArray<int>;
if ( *itype == 'c' ) ca = new ZArray<char>;
if ( *itype == 'f' ) ca = new ZArray<float>;

// I then give that class some commands, and it works based on it's own data type.


}


So, what you're saying, is that I should change this to...

  
STAMP T DoProgram (int argc, char* argv[]); ;
/* ============= */
int main(int argc, char* argv[]) {
return DoProgram<int>(argc, argv);
}


... right?



Edited by - wyrframe on February 18, 2002 3:47:29 PM

Share this post


Link to post
Share on other sites
why do you think a function must be templated in order to use template classes?
  
int DoProgram (int argc, char* argv[])
{
ZArray *ca;
char itype[16]; // why do you use an array here?

// put a value in itype

if ( *itype == ''i'' ) ca = new ZArray<int>;
if ( *itype == ''c'' ) ca = new ZArray<char>;
if ( *itype == ''f'' ) ca = new ZArray<float>;
}

that will work fine

Share this post


Link to post
Share on other sites
quote:
Original post by Wyrframe
So, what you''re saying, is that I should change this to...


STAMP T DoProgram (int argc, char* argv[]); ;
/* ============= */
int main(int argc, char* argv[]) {
return DoProgram(argc, argv);
}



Since main must return int, this is all a bit pointless. What is it you''re trying to achieve with templates?



--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites
If I make these changes...


      
int DoProgram (int argc, char* argv[]);

int DoProgram (int argc, char* argv[]) {
// ...

}


... when the compiler gets to here...


ZArray *ca; // This is line 100
char itype[16]; // An array of chars, aka a string.


I get these compiler errors...


Error E2102 .\..\pex2.c 100: Cannot use template 'ZArray' without specifying specialization parameters in function DoProgram(int,char * *)
Error E2040 .\..\pex2.c 100: Declaration terminated incorrectly in function DoProgram(int,char * *)
[/source]

123cs, your code will not work. It gives the exact same errors.



Edited by - wyrframe on February 18, 2002 6:22:56 PM

[edit: munged source tags screwed up the view of the page]

Edited by - Magmai Kai Holmlor on February 18, 2002 9:18:09 PM

Share this post


Link to post
Share on other sites
Where''s the definition of ZArray? It looks like you need to instantiate it as a template.

--
The Dilbert Principle: People are idiots.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!