simple Question

Started by
15 comments, last by Zahlman 18 years, 9 months ago
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
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++.
.
ok is there a way u can not pass in anything, for example if im returning a null.
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;

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.
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.
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
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'
node * llist::somfunc()
{
node *n=new node;
return n;
}


I think you forgot the * in the return type. Both files need correction I believe.
.
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'

This topic is closed to new replies.

Advertisement