.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()
{ ;
}
Re: .cpp and .h file not compiling
should be:
Also, please give a meaningfull name to your class.
Re: .cpp and .h file not compiling
And constructor implementation is not correct...
Re: .cpp and .h file not compiling
It's also unwise (though not technically incorrect) to put a "using" statement in a header file.
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.
Re: .cpp and .h file not compiling
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?
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.
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?
Re: .cpp and .h file not compiling
Quote:
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.
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.
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
Re: .cpp and .h file not compiling
Quote:
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.