Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Function returning a struct (http://www.chiefdelphi.com/forums/showthread.php?t=81185)

Rafael Vasquez 26-01-2010 16:27

Function returning a struct
 
I am having some issues with structure handling. The error I'm getting reads:

expected initializer before 'function_name'

Here is some sample code for what I'm trying to do:

struct my_struct{
int a;
int b;
};

.....
//This next line is the line in which the error occurs
my_struct function_name(my_struct my_argument){
.....
}

Does anyone have any ideas on what is causing this and how to fix it?

slavik262 26-01-2010 16:36

Re: Function returning a struct
 
It would help if we saw a better context for the situation. Could you post more, like maybe the whole file, and point out where the error occurs?

gvarndell 26-01-2010 20:30

Re: Function returning a struct
 
Is this in a .c file?
Or .cpp?

Rafael Vasquez 26-01-2010 22:39

Re: Function returning a struct
 
This is a .cpp file. I don't like adding context to questions like these because I am really after more general answers. In this particular scenario, there is no more context as it is a question that arose while I was teaching some of the younger members of the team about structs. The code above is essentially what I had written in the program to test this out.

slavik262 26-01-2010 22:55

Re: Function returning a struct
 
Is that code inside of a class? I really can't help unless you show more of what's going on and I don't feel like playing 20 Questions. There's nothing wrong with what you posted, but it just shows very basic form.

Tom Bottiglieri 27-01-2010 00:48

Re: Function returning a struct
 
Quote:

Originally Posted by Rafael Vasquez (Post 907342)
I am having some issues with structure handling. The error I'm getting reads:

expected initializer before 'function_name'

Here is some sample code for what I'm trying to do:

struct my_struct{
int a;
int b;
};

.....
//This next line is the line in which the error occurs
my_struct function_name(my_struct my_argument){
.....
}

Does anyone have any ideas on what is causing this and how to fix it?

Code:

struct my_struct fuctionname(struct my_struct argName)
Or you can typedef the struct.

gvarndell 27-01-2010 06:00

Re: Function returning a struct
 
Quote:

Originally Posted by Rafael Vasquez (Post 907565)
This is a .cpp file. I don't like adding context to questions like these because I am really after more general answers.

Ok, but it's clearly being compiled as C code, and that's why more context would have been helpful.

In your original post, you wrote...

Quote:

I am having some issues with structure handling. The error I'm getting reads:

expected initializer before 'function_name'
In this case, the simple answer is, if the code you posted was compiled as C++, you wouldn't get any such error because 'my_struct' becomes implicitly typedef'd.

Which means, IMHO, you are using a C compiler to teach C++ to others.:ahh:

slavik262 27-01-2010 06:53

Re: Function returning a struct
 
Quote:

Originally Posted by TomBot (Post 907665)
Code:

struct my_struct fuctionname(struct my_struct argName)
Or you can typedef the struct.

But as gvarndell said, this isn't necessary in a C++ compiler. Something funny is going on here...


All times are GMT -5. The time now is 12:14.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi