|
-
May 30th, 2009, 02:25 AM
#1
.cpp and .h file not compiling
here is the code for my .cpp and .h files, they are not compiling, i am getting the error in .cpp file in the line #include "Class.h"
The error message is
3 class.cpp Class.h:6: parse error before `;'
Please help!
Class.h
Code:
#ifndef Class_H
#define Class_H
using namespace std;
class Class
{
private;
char class_name;
char no_of_subjects;
public:
Class();
int findtotal();
} ;
#endif
Class.cpp
Code:
#include <iostream>
#include "Class.h"
Class::Class Class()
{ ;
}
int Class::findtotal()
{ ;
}
-
May 30th, 2009, 02:38 AM
#2
Re: .cpp and .h file not compiling
should be:
Also, please give a meaningfull name to your class.
-
May 30th, 2009, 02:43 AM
#3
Re: .cpp and .h file not compiling
And constructor implementation is not correct...
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 30th, 2009, 09:50 AM
#4
Re: .cpp and .h file not compiling
It's also unwise (though not technically incorrect) to put a "using" statement in a header file.
-
May 31st, 2009, 01:30 AM
#5
Re: .cpp and .h file not compiling
I overlooked the constructor syntax. Thanks for pointing out. Its compiling now. I included the namespace std, because i saw this syntax somewhere,infact i am not clear about what it does, i want to know "what exactly is namespace doing in this case?", please answer.
-
May 31st, 2009, 02:06 AM
#6
Re: .cpp and .h file not compiling
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
May 31st, 2009, 02:39 AM
#7
Re: .cpp and .h file not compiling
The link you posted is helpful but why is it so that i never used "namespace std" in any of my programs, which had cout and cin keywords and they all compiled without error. I just included the "isotream" header file.
This is the first time hen i worte a separate header file and used the namspace std, Why?
-
May 31st, 2009, 03:38 AM
#8
Re: .cpp and .h file not compiling
As long as you are using a recent compiler, you have to use the full name, like std::cout and std::cin. Otherwise, you need "using namespace std" in your code. One unlikely possibility is that you may be using a old compiler that is not compliance with C++90 standard.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
June 1st, 2009, 12:02 AM
#9
Re: .cpp and .h file not compiling
Now this is giving the error
[Linker error] undefined reference to `WinMain@16'
I have not created any "main.cpp", because it is not required by the teacher in the assignment. Will the program compile without the "main()" function, also if i create a "main.cpp" do i include the Class.cpp file in it only or the Class.h file too?
-
June 1st, 2009, 01:20 AM
#10
Re: .cpp and .h file not compiling
 Originally Posted by emerald09
I have not created any "main.cpp", because it is not required by the teacher in the assignment. Will the program compile without the "main()" function, also if i create a "main.cpp" do i include the Class.cpp file in it only or the Class.h file too?
You do not necessarily need the global main function, e.g., you can compile individual source files separately into object files. However, you need the global main function to have an executable program, but of course you are not required to name the source file containing the global main function "main.cpp".
You probably would want to define a global main function that allows you to test your class. The source file containing the definition of the global main function would need to include your class' header file, but not the source file.
-
June 1st, 2009, 02:23 AM
#11
Re: .cpp and .h file not compiling
Console applications require main(); Windows (non-console) applications require WinMain(). Make sure you've got the right type of project.
-
June 1st, 2009, 03:27 AM
#12
Re: .cpp and .h file not compiling
It seems me you use Dev-C++. In this program (as I remember) there should not be any .cpp files with the same name as the project name. You may use clear MinGW with no Dev-C++
g++ your_file_1.cpp your_file_2.cpp your_file_3.cpp -o your_program.exe
Note: no .h files in a list
-
June 1st, 2009, 10:07 AM
#13
Re: .cpp and .h file not compiling
 Originally Posted by andrey_zh
It seems me you use Dev-C++. In this program (as I remember) there should not be any .cpp files with the same name as the project name.
I've used Dev-C++ in the past and never had problems having a .cpp file with the same name as the project.
Old Unix programmers never die, they just mv to /dev/null
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|