This is the first time I'm trying to compile code that I've written using a main program file, a .h file and a .cpp implementation file. I'm using Dev C++ 4.9.9.2 and when I try to compile my main.cpp file, I get several errors of this sort.


1 F:\COP1335C\LAB#3\Time.cpp In file included from F:\COP1335C\LAB#3\Time.cpp

24 F:\COP1335C\LAB#3\Time.h expected unqualified-id before "int"

24 F:\COP1335C\LAB#3\Time.h expected `)' before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected type-name

9 F:\COP1335C\LAB#3\Time.cpp expected unqualified-id before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected `)' before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected `,' or `;' before "int"

I did a google search on some of these errors and it was suggested to place include #<iostream> and using namespace std; into my .h file, but it didn't make a difference.

Also, there were example files from class that I used dev c++ to compile and it worked fine without namespace or iostream in the .h file, so there has to be an error either in my code or I'm missing something trying to get these files to work together to make an executable. All of the files for the program are in the same directory.

Time.cpp is the implementation file and I have #include "Time.h", #include<iostream> and
using namespace std in it at the top. In my .h file, I have #ifndef TIME_H
#define TIME_H, my class definitions and then #endif at the very end.

Any suggestions?

Thanks.

Recommended Answers

All 5 Replies

don't use the name time.h -- that's the name of standard c include header file. Name it something else.

If that doesn't fix the problems then post the code.

This is the first time I'm trying to compile code that I've written using a main program file, a .h file and a .cpp implementation file. I'm using Dev C++ 4.9.9.2 and when I try to compile my main.cpp file, I get several errors of this sort.


1 F:\COP1335C\LAB#3\Time.cpp In file included from F:\COP1335C\LAB#3\Time.cpp

24 F:\COP1335C\LAB#3\Time.h expected unqualified-id before "int"

24 F:\COP1335C\LAB#3\Time.h expected `)' before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected type-name

9 F:\COP1335C\LAB#3\Time.cpp expected unqualified-id before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected `)' before "int"

9 F:\COP1335C\LAB#3\Time.cpp expected `,' or `;' before "int"

I did a google search on some of these errors and it was suggested to place include #<iostream> and using namespace std; into my .h file, but it didn't make a difference.

Also, there were example files from class that I used dev c++ to compile and it worked fine without namespace or iostream in the .h file, so there has to be an error either in my code or I'm missing something trying to get these files to work together to make an executable. All of the files for the program are in the same directory.

Time.cpp is the implementation file and I have #include "Time.h", #include<iostream> and
using namespace std in it at the top. In my .h file, I have #ifndef TIME_H
#define TIME_H, my class definitions and then #endif at the very end.

Any suggestions?

Thanks.

We'd have to see the actual code, but I do notice that in one place in your writeup you have this:

include#<iostream>

This is incorrect. You want:

#include <iostream>
using namespace std;

which you mentioned elsewhere that you have. The second way is correct, the first is incorrect. Make sure you have the second. Beyond that it's hard to say without seeing the actual code. Make sure you have a semicolon, not a comma, after using namespace std , as above. We really need to see the code though, at least up until the first function, to get a handle on it.

Sorry, I do have #include <iostream> and using namespace std; in both my implementation .cpp file and main, I just mistyped it. Here's the code up to the first two functions in my implementation file.

#include "Timenow.h"
#include <iostream>
using namespace std;
//Time.cpp
//implementation file for Time class

//setter functions
class Time::setTime(int h, int m)
{
  hour = h;
  minute = m;
  
}

void Time::setHour(int h)
{
  hour = h;
}

void Time::setMinute(int m)
{
  minute = m;
}

Sorry, I do have #include <iostream> and using namespace std; in both my implementation .cpp file and main, I just mistyped it. Here's the code up to the first two functions in my implementation file.

#include "Timenow.h"
#include <iostream>
using namespace std;
//Time.cpp
//implementation file for Time class

//setter functions
class Time::setTime(int h, int m)
{
  hour = h;
  minute = m;
  
}

void Time::setHour(int h)
{
  hour = h;
}

void Time::setMinute(int m)
{
  minute = m;
}

Try replacing class with void .

class Time::setTime(int h, int m)

That was it! Everything else is void functions that assign values to specific data members of the Time class, but for the setTime class it was described as needing to assign a time to an object variable of that class so I assumed the return type also had to be a class.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.