MOSES AMAECHI
MOSES AMAECHI

Reputation: 7

I tried to compile my code and got error `time display in class TIME does not name a type`

I created a new class called TIME and tried to define a method (time_display) in another .cpp file. I added the header file 'time.h' where the class TIME was defined. I tried to compile this code, but got an error message time display in class TIME does not name a type in line 6.

#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>
#include "time.h"

class TIME::time_display() {
    // Program to print digital clock using graphics goes here
    return 0;
}

This is my header file. I changed the file name but it still produces the same error.

#include <iostream>
#include <chrono>
#include <ctime>
#include <cstdlib>

class TIME {
    int seconds, minutes, hours;
public:
    void time_display();
};

Upvotes: 0

Views: 464

Answers (1)

wschopohl
wschopohl

Reputation: 1732

I think your mixing up a couple of things here.

Your class declaration in time.h should be something like this:

class TIME {       
  public:             
    void time_display(); 
};

Your class method definition should be something like this. Let's call that file TIME.cpp.

#include "time.h"

void TIME::time_display() {
    // void function doesn't return anything
}

So now you may have three files: main.cpp, TIME.cpp and time.h. To compile it use this for example:

g++ TIME.cpp main.cpp -o time_display

To get an executable named time_display

Upvotes: 3

Related Questions