2017-08-14 70 views
1

我有三个文件:包括问题:“多重定义”,“先在这里定义的”

main.cpp 
MyClass.cpp 
MyClass.hpp 

我有一个库的头文件,"testLib.hpp",我想在MyClass.hpp包括这样我可以有一个testLib的对象是类属性。

我在MyClass.cppmain.cpp中包括MyClass.hpp。当尝试编译项目,我收到以下错误

MyClass.cpp multiple definition of 'testLib::testLib::function1() 
obj/Release/main.o:main.cpp first defined here 
MyClass.cpp multiple definition of 'testLib::testLib::function2() 
obj/Release/main.o:main.cpp first defined here 

等。

main.cppMyClass.cpp都包括MyClass.hpp(其包括testLib.hpp)。从错误判断,看起来MyClass.cpp正试图在库函数已包含在main.cpp之后。不过,我已经包括了目前在0​​的警卫,所以我不明白它是如何试图包括MyClass.hpp两次。

下面的代码:

MyClass.hpp

#ifndef THIS_HEADER_H 
#define THIS_HEADER_H 

#include <stdint.h> 
#include <iostream> 
#include "testLib/testLib.hpp" 

class MyClass 
{ 

public: 
    void test(); 
    int foo; 

private: 
    uint32_t bar; 
    //I want to include an object from the library as part of this class 
    //TestLib::Device device; 
}; 

#endif 

MyClass.cpp

#include <stdio.h> 
#include "MyClass.hpp" 

void MyClass::test() 
{ 

} 

的main.cpp

#include <iostream> 
#include "MyClass.hpp" 

using namespace std; 

int main() 
{ 
    cout << "Hello world!" << endl; 
    return 0; 
} 

任何帮助将不胜感激!

编辑 我试图隐藏实际的文件名,使问题更普遍的和明确的,但似乎问题可能来自“testLib.hpp”,这是我没有写来产生。该文件实际上是以下“sweep.hpp”文件。我得到了“的多个定义/在这里先定义”的错误为每个在此文件中的公共职能:

sweep.hpp

#ifndef SWEEP_DC649F4E94D3_HPP 
#define SWEEP_DC649F4E94D3_HPP 

/* 
* C++ Wrapper around the low-level primitives. 
* Automatically handles resource management. 
* 
* sweep::sweep - device to interact with 
* sweep::scan - a full scan returned by the device 
* sweep::sample - a single sample in a full scan 
* 
* On error sweep::device_error gets thrown. 
*/ 

#include <cstdint> 
#include <memory> 
#include <stdexcept> 
#include <vector> 

#include <sweep/sweep.h> 

namespace sweep { 

// Error reporting 

struct device_error final : std::runtime_error { 
    using base = std::runtime_error; 
    using base::base; 
}; 

// Interface 

struct sample { 
    const std::int32_t angle; 
    const std::int32_t distance; 
    const std::int32_t signal_strength; 
}; 

struct scan { 
    std::vector<sample> samples; 
}; 

class sweep { 
public: 
    sweep(const char* port); 
    sweep(const char* port, std::int32_t bitrate); 
    void start_scanning(); 
    void stop_scanning(); 
    bool get_motor_ready(); 
    std::int32_t get_motor_speed(); 
    void set_motor_speed(std::int32_t speed); 
    std::int32_t get_sample_rate(); 
    void set_sample_rate(std::int32_t speed); 
    scan get_scan(); 
    void reset(); 

private: 
    std::unique_ptr<::sweep_device, decltype(&::sweep_device_destruct)> device; 
}; 

// Implementation 

namespace detail { 
struct error_to_exception { 
    operator ::sweep_error_s*() { return &error; } 

    ~error_to_exception() noexcept(false) { 
    if (error) { 
     device_error e{::sweep_error_message(error)}; 
     ::sweep_error_destruct(error); 
     throw e; 
    } 
    } 

    ::sweep_error_s error = nullptr; 
}; 
} 

sweep::sweep(const char* port) 
    : device{::sweep_device_construct_simple(port, detail::error_to_exception{}), &::sweep_device_destruct} {} 

sweep::sweep(const char* port, std::int32_t bitrate) 
    : device{::sweep_device_construct(port, bitrate, detail::error_to_exception{}), &::sweep_device_destruct} {} 

void sweep::start_scanning() { ::sweep_device_start_scanning(device.get(), detail::error_to_exception{}); } 

void sweep::stop_scanning() { ::sweep_device_stop_scanning(device.get(), detail::error_to_exception{}); } 

bool sweep::get_motor_ready() { return ::sweep_device_get_motor_ready(device.get(), detail::error_to_exception{}); } 

std::int32_t sweep::get_motor_speed() { return ::sweep_device_get_motor_speed(device.get(), detail::error_to_exception{}); } 

void sweep::set_motor_speed(std::int32_t speed) { 
    ::sweep_device_set_motor_speed(device.get(), speed, detail::error_to_exception{}); 
} 

std::int32_t sweep::get_sample_rate() { return ::sweep_device_get_sample_rate(device.get(), detail::error_to_exception{}); } 

void sweep::set_sample_rate(std::int32_t rate) { 
    ::sweep_device_set_sample_rate(device.get(), rate, detail::error_to_exception{}); 
} 

scan sweep::get_scan() { 
    using scan_owner = std::unique_ptr<::sweep_scan, decltype(&::sweep_scan_destruct)>; 

    scan_owner releasing_scan{::sweep_device_get_scan(device.get(), detail::error_to_exception{}), &::sweep_scan_destruct}; 

    auto num_samples = ::sweep_scan_get_number_of_samples(releasing_scan.get()); 

    scan result; 
    result.samples.reserve(num_samples); 

    for (std::int32_t n = 0; n < num_samples; ++n) { 
    auto angle = ::sweep_scan_get_angle(releasing_scan.get(), n); 
    auto distance = ::sweep_scan_get_distance(releasing_scan.get(), n); 
    auto signal = ::sweep_scan_get_signal_strength(releasing_scan.get(), n); 

    result.samples.push_back(sample{angle, distance, signal}); 
    } 

    return result; 
} 

void sweep::reset() { ::sweep_device_reset(device.get(), detail::error_to_exception{}); } 

} // ns 

#endif 
+0

您是否尝试过#pragma一次 –

+5

包含防护措施会阻止将同一个头文件包含在同一个源中两次。它们不会阻止将相同的头文件包含到多个源文件中(它们也不应该;这正是头文件的预期用法)。主要的错误是'testLib.hpp'头文件定义了几个函数。一个头文件只应该声明函数(应该在某个源文件中定义);或者用'inline'关键字来定义它们。 –

+0

@IgorTandetnik感谢您的回复。我在原始文章中包含了我命名为'testLib.hpp'的实际库文件(sweep.hpp)。我没有写sweep.hpp。看起来他们确实声明了一些我得到错误的功能。所以你说这个错误完全来自sweep.hpp? – user3878723

回答

1

你的问题的一个简化版本:

马车。 HPP

int function() { return 0; } 

的main.cpp

#include "buggy.hpp" 
int main() { return 0; } 

other.cpp

#include "buggy.hpp" 

的问题是,buggy.hpp定义function,不只是宣布。扩展标题包含之后,这意味着在main.cppother.cpp中声明了function - 这是不允许的。

修复方法是声明functioninline,它允许在多个翻译单元中声明函数。

inline int function() { return 0; } 

事实上,允许多个定义是inline到C++ 标准含义。编译器可以将它视为暗示函数体可以在线扩展。好的不会;他们更擅长做出那种程序员的决定)。