Separating my classes into different header file in c++

I have a problem with separating my classes into one header and one cpp file. Below is my main cpp file:

 #include "myclasses.h"     
 #include <math.h>                              
 #include <string.h>    
 #include "stdafx.h"
 #include <fstream>                             
 #include <stdio.h>                             
 #include <stdlib.h>
 #include <vector>
  .....my other headers....
 const int populationsize = 10;

Chrom temp;
chrom popcurrent[populationsize];
chrom poptemp;
chrom popnext[populationsize];
chrom popnextxover[populationsize];

And then couple of function definitions and then int main {body} I have myclasses.h header file as follows:

#ifndef myclasses_h
#define myclasses_h
#include <vector>
#include <iostream>
#include <fstream>
#include <stdafx.h>
using namespace std;
int Machines, Jobs,jobscp;
typedef class solutionset
{
public:
vector <int> indexinmilp;
vector <int>  indexinga;

vector <int>  startinmilp;
void resize();
};
solutionset problemsolution;
typedef class Chrom                                 
{
public:
vector<vector <float>> StartTime;
vector<vector <int>> Processing;
vector<vector < int>> t1;
int M;
int W;
float fit;
void variablesresize();
} ;

class Problem{
public:
vector<vector <int>> Processing;
vector<vector < int>> t1;
int M;
int W;

void setvectorsize();
};
#endif

And I defined the myclasses.cpp as follows:

#include "myclasses.h"
#include <vector>
#include <stdafx.h>
#include <iostream>
#include <fstream>
using namespace std;
vector< int> indexinmilp;
vector< int> indexinga;
vector <int>  startinmilp;
void solutionset::resize(){
    for (int i = 0; i < Machines - 1; ++i)              
    {
        indexinmilp.resize(jobscp);
        indexinga.resize(jobscp);
        startinmilp.resize(jobscp);
    }

};
solutionset problemsolution;


  void Chrom::variablesresize(){

    int i = 0, j, k;
    float a;
    std::vector<float> datapoints;

    std::ifstream myfile("Input.dat", std::ios_base::in);
    i = 0;                  
    myfile.open("Input.dat");                   
    while (!myfile.eof())
    {
        myfile >> a;
            if (!myfile)                
        {
            myfile.clear();             
            myfile.ignore(1);           
        }
        else
        {

            datapoints.push_back(a);
            ++i;
        }
    }

    myfile.close();
    Jobs = datapoints[0];
    Machines = datapoints[1];
    jobisconsidered.resize(Machines);
    StartTime.resize(Machines)                  

    for (int i = 0; i < Machines; ++i)

    {
        StartTime[i].resize(Jobs);  
        jobisconsidered[i].reserve(Jobs);

    }
}

void Problem::setvectorsize()
{ 
    Processing.resize(Machines);
    t1.resize(Machines);

    for (int i = 0; i < Machines; ++i)
    {
        Processing[i].resize(Jobs);
        t1[i].resize(Jobs);
    }

};

However I see couple of errors when I run it in visual studio. I check other threads in related to this question such as Difference between implementing a class inside a .h file or in a .cpp file and still cannot figure it what is the problem. In addition, I have some global variables but it seems I need to redefine them in myclasses.h header file and myclasses.cpp

Errors that I get: First for myclasses.cpp: For command vector< int> indexinmilp; Errors include

Error 3 error C2143: syntax error : missing ';' before '<'
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

For command vector< int> indexinga; Errors include

Error 5 error C2143: syntax error : missing ';' before '<'
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

For command vector< int> indexinga; Errors include:

Error 7 error C2086: 'int vector' : redefinition Error 8 error C2143: syntax error : missing ';' before '<'

For command indexinga.resize(jobscp); errors include:

Error 15 error C2228: left of '.resize' must have class/struct/union Error 16 error C2065: 'startinmilp' : undeclared identifier

Or in command void Chrom::variablesresize(){ I get below error:

Error 20 error C2653: 'Chrom' : is not a class or namespace name

Actually there are more than 50 errors. Maybe I made some mistake.The code was working well before this separation.

Any help is highly appreciated. Regards

Upvotes: 1

Views: 184

Answers (1)

D.Go
D.Go

Reputation: 171

  1. You should not declare global variables in your header. You are creating a conflict between main.cpp and myclasses.cpp. Look up the "extern" keyword for referencing global variables.

  2. You redefine problemsolution in myclasses.cpp.

  3. You should not use "typedef class". You use that twice but not on the third class.

  4. As stated by others, you need to provide the error details.

Upvotes: 2

Related Questions