PDA

View Full Version : C++ compilation



santanu.csc
September 16th, 2010, 06:19 AM
when I compile the following program in Anjuta IDE

#include<iostream>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
void person : : getdata(void)
{
cout << "Enter Name";
cin >> name;
cout << "Enter age";
cin >> age;
}
void person : : display(void)
{
cout << "\nName : "<< name;
cout << "\nAge : "<<age;
}
int main()
{
person p;
p.getdata();
p.display();
return 0;
}

Then the following error Occour

a.cxx:10: error: function definition does not declare parameters
a.cxx:17: error: function definition does not declare parameters

Please Help Me.........How to compile and also all steps clearly..
Thankfully
Santanu

NovaAesa
September 16th, 2010, 06:34 AM
I'll give you a hint, you have a problem where you are trying to fully qualify the functions you are defining.

Do you understand what the "person : : " part before getdata(void) is meant to do?

santanu.csc
September 16th, 2010, 06:50 AM
Please Say clearly

NovaAesa
September 16th, 2010, 07:21 AM
I've given you a hint towards solving your problem. You still haven't answered my question "Do you understand what the 'person : : ' part before getdata(void) is meant to do?".

Did you write the code yourself or is it an example from a book or the Internet?

ChurroLoco
September 16th, 2010, 08:39 AM
You are mixing up your declaration with your definition. Classes in C++ should generally consist of two file. The .cpp (Implementation file) and .h (Declaration File).

Here is a little blurb about declaration/definition
http://www.edaboard.com/thread124112.html

You are mostly showing what should be in the .h file the problem is you are also trying to say what the function do in this file. You are only supposed to list there names, return type, and parameters.

so

void GetData();
[Notic you don't need "Person::" in the header because your within the class curly braces]

In a seporate c++ file you should include this header and fill in the implementation. for each function. So something like:


#include "person.h"

void person::getdata()
{
cout << "Enter Name";
cin >> name;
cout << "Enter age";
cin >> age;
}

//...The rest of your person:: method definitions go here


Check this page out to learn more about C++ classes.
http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html

Hope this helps

vijushimpi
September 16th, 2010, 09:00 AM
the above program successfully complied under code::blocks IDE, I just added using namespace std;

NovaAesa
September 16th, 2010, 09:52 AM
the above program successfully complied under code::blocks IDE, I just added using namespace std;
That's rather interesting, because it doesn't compile with g++ even after adding using namespace std. Which compiler are you using with code::blocks?

dwhitney67
September 16th, 2010, 09:55 AM
You are mixing up your declaration with your definition. Classes in C++ should generally consist of two file. The .cpp (Implementation file) and .h (Declaration File).

Incorrect information never helps.

dwhitney67
September 16th, 2010, 10:00 AM
That's rather interesting, because it doesn't compile with g++ even after adding using namespace std. Which compiler are you using with code::blocks?

Agreed; the compiler will definitely complain about a syntax error. Here are the errors I get:


g++ -Wall -pedantic ubuntu.cpp
ubuntu.cpp:11: error: expected initializer before : token
ubuntu.cpp:18: error: expected initializer before : token

This tells me that the syntax of ": :" should be "::" (ie no white-space allowed).

schauerlich
September 16th, 2010, 10:04 AM
There are only a few small mistakes which are simple and easy to fix; they have all been highlighted in this thread. Look at the line numbers the compiler gives, and try to figure out what's wrong. Hint: ':' and '::' have quite different meanings in C++.

EDIT: looks like dwhitney ninja'd me with the answer... oh well.

vijushimpi
September 22nd, 2010, 04:55 AM
when I copied from website to IDE it automatically converted ": :" into "::" that is why I unaware of it, by the way I am using default compiler ie, G++