newbie question

N

Neo

I wrote the following code to learn about structs(ok I know classes are
better) but I couldnt understand why the mystruct declaration and the
functions getval and display have to be before main(). I got compile
errors when I declared them in main() function. why not after main() as
I feel thay are similar to variable declarations. please let me know:
other than preprocessor commands and globals, whats the rule for things
appearing before main() and after main() ??
---------code------------------------------------------------
#include <iostream>

using namespace std;

typedef struct mystruct {
int i;
char c;
} ic_struct;
void getval (ic_struct* a, int b, char c) {
a->i= b;
a->c= c;
}
void display (ic_struct* x) {
cout << "x.i value= " << x->i << endl \
<< "x.c value= " << x->c << endl;
}

int main () {

ic_struct st1;
ic_struct* p_st1 = &st1;
getval(p_st1, 3, 'q');
display(p_st1);

return 0;
}
 
A

Allan Bruce

Neo said:
I wrote the following code to learn about structs(ok I know classes are
better) but I couldnt understand why the mystruct declaration and the
functions getval and display have to be before main(). I got compile
errors when I declared them in main() function. why not after main() as
I feel thay are similar to variable declarations. please let me know:
other than preprocessor commands and globals, whats the rule for things
appearing before main() and after main() ??

You must declare anything before use in C/C++. This is either by merely
saying that a given function/method exists, or by fully defining it. In
your example you could have the following line before your main entry point:

void getval (ic_struct* a, int b, char c);

Then you can put the actual function definition after your main. A similar
approach can be used for structs but I dont recommend it as it looks untidy
IMO.

Allan

<code snipped>
 
H

Howard

Neo said:
I wrote the following code to learn about structs(ok I know classes are
better)

There's nothing particularly "better" about classes than structs. They both
have the same capabilities. The difference is in the default visibility of
the members, and the default inheritance. Perhaps you're looking at (or
recalling) some very early C++ implementation, where structs could only hold
data, and classes could add member functions.
but I couldnt understand why the mystruct declaration and the
functions getval and display have to be before main(). I got compile
errors when I declared them in main() function. why not after main() as
I feel thay are similar to variable declarations. please let me know:
other than preprocessor commands and globals, whats the rule for things
appearing before main() and after main() ??

I never put anything after main, personally. But in C++, you have to
declare an object before you can use it, and placing it after main means
it's not yet declared. If you really want to put the body of a function
after main (because you want main at the top, for instance, for
readability), then you need to first declare the function before main, and
then define it (put the actual code body in) afterwards.
---------code------------------------------------------------
#include <iostream>

using namespace std;

typedef struct mystruct {
int i;
char c;
} ic_struct;


This is an old C-style struct definition. In C++, just do this:

struct ic_struct
{
int i;
char c;
};
void getval (ic_struct* a, int b, char c) {
a->i= b;
a->c= c;
}

This might more accurately be call "SetVals", since it sets two values
instead of getting one. But you should look at using constructors. That's
the C++ way of initializing objects. And use member functions to set or
access data values once the object is created.
void display (ic_struct* x) {
cout << "x.i value= " << x->i << endl \

Unless you're defining a macro, don't put that trailing \ in there. C++ is
not line-oriented.
<< "x.c value= " << x->c << endl;
}

int main () {

ic_struct st1;
ic_struct* p_st1 = &st1;
getval(p_st1, 3, 'q');
display(p_st1);

Did you introduce p_st1 just for practice? If not, you could just pass &st1
to those functions instead of using a pointer variable.
return 0;
}

Your best bet would be a good C++ book (or three). Also, you might check
the FAQ, at http://www.parashift.com/c++-faq-lite/ , which has a lot of good
C++ advice.


-Howard
 
H

Howard

Neo said:
I wrote the following code to learn about structs(ok I know classes are
better) but I couldnt understand why the mystruct declaration and the
functions getval and display have to be before main(). I got compile
errors when I declared them in main() function. why not after main() as
I feel thay are similar to variable declarations. please let me know:
other than preprocessor commands and globals, whats the rule for things
appearing before main() and after main() ??

Oh, I see now that you meant after the *start* of the main function, not
after the *end* of the main function. In other words, you wanted to put
functions inside the body of the main function itself. That's just not
allowed in C++. Functions have to go outside. But the best place for ones
like you've got is as part of the object they work on, as member functions.
And member functions can be defined either outside the class definition,
just like a non-member function, or "in-line", in the class definition
itself.

-Howard
 
D

DarkSpy

I wrote the following code to learn about structs(ok I know classes are
better) but I couldnt understand why the mystruct declaration and the
functions getval and display have to be before main(). I got compile
errors when I declared them in main() function. why not after main() as
I feel thay are similar to variable declarations. please let me know:
other than preprocessor commands and globals, whats the rule for things
appearing before main() and after main() ??
you'd not allocate the memory for pointer before you use it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,507
Members
48,193
Latest member
DannyRober

Latest Threads

Top