1

I am trying to do use a structure and the function defining the structure in different files. As suggested here I am doing the following:

I define my struct and save it in the file agent.h

// File agent.h

#ifndef AGENT_H
#define AGENT_H
#include "stdafx.h"
#include <random>
#include <vector>
#include <iostream>

// Define Nodes and Agents
struct Agent
{
    int home, work; // Locations
    int status; // S=0; E=1; I=2; R=3
    Agent *initialize_agents(int N, int V);
    Agent()
    {
        status = 0;
    }
}A;
#endif

I defined the function the function and saved it as agent.cpp

// File agent.cpp
#include "stdafx.h"
#include <random>
#include <vector>
#include <iostream>
#include "Agent.h"
using namespace std;
Agent *initialize_agents(int N, int V)
{
    Agent  *A = new Agent[N];
    char fileN[1024] =  "myFile.dat";
    FILE *f = fopen(fileN, "r"); // Binary File Home Work
    int k = 0;
    int v = 0;
    while (!feof(f))
    {
         int i, j;
         fscanf(f, "%d %d", &i, &j);
         A[k].home = i;
         A[k].work = j;
         k++;
    }
   return(A);
}

then I have the main file main.cpp

// File agent.cpp
#include "stdafx.h"
#include <vector>
#include <iostream>
#include "agent.h"
using namespace std;
int main()
{
    int Inet;
    struct Agent;
    int V = 100;
    int N = 100;
    Agent *A = initialize_agents(N, V); // Initialize Agents
    return 0;
}

and I got the following error:

error: 'initialize_agents' was not declared in this scope
emax
  • 6,965
  • 19
  • 74
  • 141

1 Answers1

0

Here's your code fixed to compile -

agent.h:

// File agent.h

#ifndef AGENT_H
#define AGENT_H
#include "stdafx.h"
#include <random>
#include <vector>
#include <iostream>

// Define Nodes and Agents
struct Agent
{
    int home, work; // Locations
    int status; // S=0; E=1; I=2; R=3
    Agent()
    {
        status = 0;
    }
};

Agent *initialize_agents(int N, int V);
#endif

agent.cpp:

// File agent.cpp
#include "stdafx.h"
#include <random>
#include <vector>
#include <iostream>
#include "Agent.h"
using namespace std;
Agent *initialize_agents(int N, int V)
{
    Agent  *A = new Agent[N];
    char fileN[1024] = "myFile.dat";
    FILE *f = fopen(fileN, "r"); // Binary File Home Work
    int k = 0;
    int v = 0;
    while (!feof(f))
    {
        int i, j;
        fscanf(f, "%d %d", &i, &j);
        A[k].home = i;
        A[k].work = j;
        k++;
    }
    return(A);
}

main.cpp:

// File agent.cpp
#include "stdafx.h"
#include <vector>
#include <iostream>
#include "agent.h"
using namespace std;
int main()
{
    int Inet;
    int V = 100;
    int N = 100;
    Agent *A = initialize_agents(N, V); // Initialize Agents
    return 0;
}
Marcin Zawiejski
  • 1,123
  • 1
  • 9
  • 23
  • It returns the following `error: cannot convert 'Agent*' to 'main()::Agent*' in initialization` – emax Feb 12 '19 at 12:14
  • Are you sure you _don't_ use "struct Agent" in your main? – Marcin Zawiejski Feb 12 '19 at 12:19
  • right I just removed it and it returns `/tmp/ccWik5Sx.o: In function 'main': main.cpp:(.text+0x74c): undefined reference to 'initialize_agents(int, int)' collect2: error: ld returned 1 exit status` – emax Feb 12 '19 at 12:25
  • It seems you didn't include agent.cpp in your build. – Marcin Zawiejski Feb 12 '19 at 12:27
  • I do have it in the same folder – emax Feb 12 '19 at 12:28
  • Having it in the same folder is not enough. You need to compile it and link the output .obj file into the executable but that's already descibed under the link you refer to in the OP. – Marcin Zawiejski Feb 12 '19 at 12:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188277/discussion-between-emax-and-marcin-zawiejski). – emax Feb 12 '19 at 12:44