【c++】不规范

博客讲述了在C++项目中遇到V-REP API报错LNK2005的问题,作者尝试将vrep类的定义从cpp文件移至头文件,以解决该问题。然而,这不符合常规的编程规范。主要内容包括V-REP连接、模拟启动、对象位置获取及路径规划等操作。作者建议记录笔记以避免遗忘编程细节。
摘要由CSDN通过智能技术生成

报错 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就忘干净了。。。。。。还是要多记笔记才行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值