CodeX
CodeX

Reputation: 17

Two errors using class and header

I get the error class redefinition

Also the error ID is not a member of class process

and missing semi colon before ID

I tried char* instead of string.. not working also

Any help? I seem to be missing something

Thanks in advance

process.h file

#ifndef PROCESS_H
#define PROCESS_H


 class process
{
public:

 string ID;
 int run_time;
 int arrival_time;

 process();

 int get_start_time();
 int get_finish_time(int start, int run);
 int get_TA_time(int finish, int start);
 float get_weighted_TA_time();

};

#endif

process.cpp file

#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>

#include "process.h"

using namespace std;

class process
{ 
public:

 process:: process() {};
 int process:: get_start_time()
 {
     int x;
     return x;
 }
 int process:: get_finish_time(int start, int run)
 {
     int x;
     return x= start +run;
 }
 int process:: get_TA_time(int finish, int start)
 {
     int x;
     return x= finish - start;
 }
 float process::  get_weighted_TA_time()
 {};
 };

Upvotes: 0

Views: 25

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 30489

Problem 1 Class refinition
In cpp file you don't need to write class process. It should be

#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>

#include "process.h"

using namespace std;

process:: process() {};
int process:: get_start_time()
{
    int x;
    return x;
}
int process:: get_finish_time(int start, int run)
{
    int x;
    return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
    int x;
    return x= finish - start;
}
float process::  get_weighted_TA_time()
{};

In header file you have given required declarations. In cpp, you need to give the definitions. But because you are outside the class scope now, you must give the fully qualified name of members which you are already doing. (for ex: int process:: get_finish_time(int start, int run))

If you again use class process, compiler has no hint that you intend the same class and not want a new class causing the class redifinition error. In C++, it is not allowed to make a class partially and complete the rest of it somewhere else. (Not to be cofused with inheritance)

Problem 2: string issue
Add #include <string> in your header file and use std::string or add line using std::string

Upvotes: 2

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

There are a few issues. One is this:

process.h needs to #include <string>. Once included, prepend all string variables with std::.

    #ifndef PROCESS_H
    #define PROCESS_H

    #include <string>
    class process
    {
        public:
           std::string ID;
           int run_time;
           int arrival_time;

           process();
           int get_start_time();
           int get_finish_time(int start, int run);
           int get_TA_time(int finish, int start);
           float get_weighted_TA_time();
   };
   #endif

The problem with your original code is that you're:

  1. Relying that some outside module included <string> before process.h is included.
  2. Relying that some outside module stated using namespace std; before including process.h.

None of those can be guaranteed.

Upvotes: 0

Related Questions