我正在尝试使用不同文件中的结构和定义结构的函数。根据建议的here,我正在执行以下操作:
我定义我的struct
并将其保存在文件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
我将函数定义为函数,并将其另存为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;
struct Agent;
int V = 100;
int N = 100;
Agent *A = initialize_agents(N, V); // Initialize Agents
return 0;
}
我收到以下错误:
error: 'initialize_agents' was not declared in this scope
答案 0 :(得分:0)
这是您要编译的代码-
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;
}