Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

simple Question

This topic is 4892 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

ok lets say I have a struct like this: struct node { char in; node *next; }; how do I make a funtion where its return type is s node

Share this post


Link to post
Share on other sites
Advertisement

node functionName(char i, node n)
{
node newNode = new node(i, n);
return newNode;
}




Does this work? Somehow I feel like I'm mixing up Java and C/C++.

Share this post


Link to post
Share on other sites
I dont know if I have understand the question correctly:)
Don't know what you mean by 'return type is s node'?


struct stNODE {
char in;
stNODE * pNext;
stNODE( char data = 0, stNODE * pNode = NULL )
{
in = data;
pNext = pNode;
}
virtual ~stNODE()
{
if(pNext)
{
delete pNext;
pNext = NULL;
}
}
};

stNODE * pNodeA = new stNODE();
stNODE * pNodeB = new stNODE('A',pNodeA);
delete pNodeB;
pNodeB = NULL;


Share this post


Link to post
Share on other sites
C:

struct node some_func() {
struct node n = blah;
return n;
}


C++:

node some_func() {
node n = blah;
return n;
}


Objects that are returned from a routine are copied, unless you return a pointer to a node residing in allocated memory.

Share this post


Link to post
Share on other sites
ya thats what I mean, im doing what u just said in C++.
but im gettin thig error:

error C2143: syntax error : missing ';' before 'llist::functionname'

ok llist is a class, and all this is happening in the class. functionname is a method.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ksingh30
ya thats what I mean, im doing what u just said in C++.
but im gettin thig error:

error C2143: syntax error : missing ';' before 'llist::functionname'

ok llist is a class, and all this is happening in the class. functionname is a method.


post your code

-me

Share this post


Link to post
Share on other sites
ok this is the header file

#ifndef __llistH__
#define __llistH__
class llist
{
public:
struct node
{
char alpha;
node *next;
};
node *list;
llist();
~llist();
node somfunc();

};
#endif



heres the cpp file

#include "llist.h"

llist::llist()
{

list=0;
}
llist::~llist()
{
}
node llist::somfunc()
{
node *n=new node;
return n;

}




the errors:
c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(11) : error C2143: syntax error : missing ';' before 'llist::somfunc'

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(11) : error C2501: 'node' : missing storage-class or type specifiers

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(12) : error C2556: 'int llist::somfunc(void)' : overloaded function differs only by return type from 'llist::node llist::somfunc(void)'

c:\Documents and Settings\Kevin\Desktop\linked\llist.h(18) : see declaration of 'llist::somfunc'

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(12) : error C2371: 'llist::somfunc' : redefinition; different basic types

c:\Documents and Settings\Kevin\Desktop\linked\llist.h(18) : see declaration of 'llist::somfunc'

Share this post


Link to post
Share on other sites
node * llist::somfunc()
{
node *n=new node;
return n;
}


I think you forgot the * in the return type. Both files need correction I believe.

Share this post


Link to post
Share on other sites
ok I added the pointer now I get these error:

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(11) : error C2143: syntax error : missing ';' before '*'

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(11) : error C2501: 'node' : missing storage-class or type specifiers

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(12) : error C2501: 'llist::somfunc' : missing storage-class or type specifiers

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(12) : error C2556: 'int *llist::somfunc(void)' : overloaded function differs only by return type from 'llist::node *llist::somfunc(void)'

c:\Documents and Settings\Kevin\Desktop\linked\llist.h(18) : see declaration of 'llist::somfunc'

c:\Documents and Settings\Kevin\Desktop\linked\llist.cpp(12) : error C2371: 'llist::somfunc' : redefinition; different basic types

c:\Documents and Settings\Kevin\Desktop\linked\llist.h(18) : see declaration of 'llist::somfunc'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!