报错 LNK 2005
解决方案 ?
因为在路径规划类里面要用到vrep的API,所以有一个方法是放在main.h里面定义的,定义中用到了已经声明的m_vrep的函数
main.h
#include "2dpointplanningdemo.h"
#include "vrep.h"
vrep m_vrep;
void Plane2DEnvironment::drawPath()
{
float x, y;
if (ss_->haveSolutionPath())
{
if (!useDeterministicSampling_)
ss_->simplifySolution();
og::PathGeometric& p = ss_->getSolutionPath();
if (!useDeterministicSampling_)
{
ss_->getPathSimplifier()->simplifyMax(p);
ss_->getPathSimplifier()->smoothBSpline(p);
}
size_t length = p.getStateCount();
//p.interpolate(20);
cout << "path length is : " << endl << length << endl;
for (int i = 0; i < length; i++)
{
x = (p.getState(i)->as<ob::RealVectorStateSpace::StateType>()->values[0]) / 100;
y = (p.getState(i)->as<ob::RealVectorStateSpace::StateType>()->values[1]) / 100;
cout << "path point : " << i << ". x : " << x << ".y : " << y << endl;
m_vrep.setpose2d(m_vrep.robot_handle, x, y);
extApi_sleepMs(10);
}
}
}
vrep类本来是头文件只声明,cpp文件里面定义的,现在改成头文件直接定义了。。。这样就没有vrep.cpp文件再include vrep.h了,但是貌似不符合规范。。。
#pragma once
#include "extApi.h"
#include "simConst.h"
#include "extApiPlatform.h"
#include "extApiInternal.h"
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;
class vrep
{
public:
void connect(const char* ip)
{
int Port = 19997;
client_id = simxStart((simxChar*)ip, Port, 1, 1, 1000, 5);
stop();
extApi_sleepMs(100);
if (client_id != -1)
{
cout << "V-rep connected.";
simxGetObjectHandle(client_id, "robot", &robot_handle, simx_opmode_blocking);
simxGetObjectHandle(client_id, "goal", &goal_handle, simx_opmode_blocking);
}
else
{
cout << "V-rep can't be connected.";
}
}
void start()
{
simxStartSimulation(client_id, simx_opmode_oneshot);
cout << "simulation started ...... " << endl;
extApi_sleepMs(100);
}
void stop()
{
simxStopSimulation(client_id, simx_opmode_oneshot);
}
void setpose2d(int handle, float x, float y)
{
float position[3] = { x, y, 0 };
simxSetObjectPosition(client_id, (simxInt)handle, -1, position, simx_opmode_oneshot);
}
void getobjectpos(int handle, float* pos)
{
simxGetObjectPosition(client_id, handle, -1, pos, simx_opmode_blocking);
extApi_sleepMs(50);
}
int client_id;
int robot_handle;
int goal_handle;
};
此时,main.h实例化了vrep类,main.c里面就用了extern vrep m_vrep;
/*********************************************************************
* @ Harbin Institute of Technology
*
* @author : HI_FORREST
*
* @date : 2021.12.28
*
*
*********************************************************************/
#include "main.h"
extern vrep m_vrep;
int main()
{
m_vrep.connect("127.0.0.1");
m_vrep.start();
std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
boost::filesystem::path path("E:/c++program/ompl/resources/resources/");
bool useDeterministicSampling = true;
Plane2DEnvironment env((path / "ppm/floor.ppm").string().c_str(), useDeterministicSampling);
float pos[3] = { 0,0,0 };
float target[3] = { 0,0,0 };
m_vrep.getobjectpos(m_vrep.robot_handle, pos);
m_vrep.getobjectpos(m_vrep.goal_handle, target);
int x = (int)(pos[0] * 100);
int y = (int)(pos[1] * 100);
int tx = (int)(target[0] * 100);
int ty = (int)(target[1] * 100);
cout << "start position : x : " << x << " . y : " << y << endl;
cout << "goal position : tx : " << tx << " . ty : " << ty << endl;
if (env.plan(x, y, tx, ty))
{
env.recordSolution();
cout << "saving path as pictur ... " << endl;
env.save("result_demo.ppm");
cout << "showing path in vrep ... " << endl;
env.drawPath();
}
return 0;
}
一个中午了,就改了改昨天的bug,新建了一个工程,三个月不用VS就忘干净了。。。。。。还是要多记笔记才行