包含问题:“多个定义”,“此处首先定义”

use*_*723 1 c++

我有三个文件:

main.cpp
MyClass.cpp
MyClass.hpp
Run Code Online (Sandbox Code Playgroud)

我有一个库头文件,"testLib.hpp"我想包含在其中,MyClass.hpp以便使testLib的对象之一成为类属性。

我包含MyClass.hppMyClass.cpp和中main.cpp。尝试编译项目时,出现以下错误

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
Run Code Online (Sandbox Code Playgroud)

等等。

两者main.cppMyClass.cpp包括MyClass.hpp(包括testLib.hpp)。从错误的角度来看,似乎MyClass.cpp已经尝试将库函数包括进来main.cpp。但是,我有警卫在场,MyClass.hpp所以我不知道它是如何试图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
Run Code Online (Sandbox Code Playgroud)

MyClass.cpp

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

void MyClass::test()
{

}
Run Code Online (Sandbox Code Playgroud)

main.cpp

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

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

编辑 我试图隐藏实际的文件名以使问题更一般和清楚,但是似乎问题可能是由于我没有写的'testLib.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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ica 7

您的问题的简化版本:

越野车

int function() { return 0; }
Run Code Online (Sandbox Code Playgroud)

main.cpp

 #include "buggy.hpp"
 int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)

other.cpp

 #include "buggy.hpp"
Run Code Online (Sandbox Code Playgroud)

问题是,buggy.hpp定义 function,不只是宣告。扩展标头包含后,即意味着functionmain.cpp和中都声明了other.cpp-并且不允许这样做。

解决方法是声明function为as inline,从而允许在多个转换单元中声明函数。

inline int function() { return 0; }
Run Code Online (Sandbox Code Playgroud)

实际上,允许多个定义是C ++ 标准唯一含义。编译器可能会将其视为函数主体可以内联扩展的暗示。好人不会;他们更擅长做出程序员决定的那种决定)。inline

  • @interstar 1. 您可以在使用它们的每个翻译单元中定义(并且实际上需要)类型(包括类)。2. 如果你把 `struct foo { int bar(); }; int foo::bar() { 返回 0; }` 在头文件中,您将遇到完全相同的问题。不同之处在于,如果您在类定义中定义成员函数 *in*,则它被隐式声明为“inline”。 (2认同)