在.cpp中包含头文件会导致令人讨厌的错误

Including header file in .cpp causes nasty error

本文关键字:讨厌 错误 cpp 包含头 文件      更新时间:2023-10-16

我有一个名为transaction_gen.h的头文件和一个名为transaction_gen.cpp的相应cpp文件。

这是我在transaction_gen.h

#ifndef BITCOIN_TEST_GEN_TRANSACTION_GEN_H
#define BITCOIN_TEST_GEN_TRANSACTION_GEN_H
#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/Gen.h>
#include "primitives/transaction.h" 
#include "script/script.h"
#include "amount.h"
#include "test/gen/script_gen.h"
#include "test/gen/crypto_gen.h"
namespace rc {
  template<>
  struct Arbitrary<COutPoint> {
    static Gen<COutPoint> arbitrary() { 
      return gen::map(gen::tuple(gen::arbitrary<uint256>(), gen::arbitrary<uint32_t>()), [](std::tuple<uint256, uint32_t> outPointPrimitives) {
          uint32_t nIn; 
          uint256 nHashIn; 
          std::tie(nHashIn, nIn) = outPointPrimitives;
          return COutPoint(nHashIn, nIn);
          });
    };
  };

  template<> 
  struct Arbitrary<CTxIn> { 
    static Gen<CTxIn> arbitrary() { 
      return gen::map(gen::tuple(gen::arbitrary<COutPoint>(), gen::arbitrary<CScript>(), gen::arbitrary<uint32_t>()), [](std::tuple<COutPoint, CScript, uint32_t> txInPrimitives) { 
        COutPoint outpoint; 
        CScript script;
        uint32_t sequence; 
        std::tie(outpoint,script,sequence) = txInPrimitives; 
        return CTxIn(outpoint,script,sequence); 
      });
    };
  };
  /** Generates one or more inputs */ 
  Gen<std::vector<CTxIn>> oneOrMoreInputs();

  template<>
  struct Arbitrary<CAmount> {
    static Gen<CAmount> arbitrary() {
      //why doesn't this generator call work? It seems to cause an infinite loop. 
      //return gen::arbitrary<int64_t>();
      return gen::inRange(std::numeric_limits<int64_t>::min(),std::numeric_limits<int64_t>::max());
    };
  }; 
  template<>
  struct Arbitrary<CTxOut> { 
    static Gen<CTxOut> arbitrary() { 
      return gen::map(gen::tuple(gen::arbitrary<CAmount>(), gen::arbitrary<CScript>()), [](std::tuple<CAmount, CScript> txOutPrimitives) {
        CAmount amount;  
        CScript script;
        std::tie(amount,script) = txOutPrimitives;
        return CTxOut(amount,script);
      });
    };
  }; 

这是我在相应的 cpp 文件中的内容

#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/Gen.h>
#include "primitives/transaction.h" 
#include "script/script.h"
#include "amount.h"
#include "test/gen/transaction_gen.h"
namespace rc { 
  /** Generates one or more inputs */ 
  Gen<std::vector<CTxIn>> oneOrMoreInputs() {
    return gen::suchThat(gen::arbitrary<std::vector<CTxIn>>(), [](std::vector<CTxIn> vin) {
      return vin.size() > 0;      
    });
  }
  /** Generates one or more outputs */ 
  Gen<std::vector<CTxOut>> oneOrMoreOutputs() { 
    return gen::suchThat(gen::arbitrary<std::vector<CTxOut>>(), [](std::vector<CTxOut> vout) {
      return vout.size() > 0;      
    });
  }
}

当我构建程序时,这将编译良好。

但是,由于我想使用位于transaction_gen.h的功能,因此我显然希望将其包含在我的程序中。但是,当我包含它时,我收到一条很长的讨厌错误消息:

chris@chris-870Z5E-880Z5E-680Z5E:~/.../src/test$ make
make -C .. bitcoin_test
make[1]: Entering directory `/home/chris/dev/bitcoin/src'
  CXX      test/test_test_bitcoin-transaction_properties.o
  CXX      test/gen/test_test_bitcoin-transaction_gen.o
In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h: In static member function ‘static rc::Gen<CKey> rc::Arbitrary<CKey>::arbitrary()’:
./test/gen/crypto_gen.h:19:8: error: no matching function for call to ‘map(rc::Arbitrary<CKey>::arbitrary()::<lambda(int)>)’
       });
        ^
In file included from /usr/local/include/rapidcheck/Gen.h:74:0,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/Gen.hpp:15:54: note: candidate: template<class T, class Mapper> rc::Gen<typename std::decay<typename std::result_of<Mapper(T)>::type>::type> rc::gen::map(rc::Gen<T>, Mapper&&)
 Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
                                                      ^
/usr/local/include/rapidcheck/Gen.hpp:15:54: note:   template argument deduction/substitution failed:
In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h:19:8: note:   candidate expects 2 arguments, 1 provided
       });
        ^
./test/gen/crypto_gen.h: In static member function ‘static rc::Gen<std::vector<unsigned char, secure_allocator<unsigned char> > > rc::Arbitrary<std::vector<unsigned char, secure_allocator<unsigned char> > >::arbitrary()’:
./test/gen/crypto_gen.h:29:8: error: no matching function for call to ‘map(rc::Arbitrary<std::vector<unsigned char, secure_allocator<unsigned char> > >::arbitrary()::<lambda(CKey)>)’
       });
        ^
In file included from /usr/local/include/rapidcheck/Gen.h:74:0,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/Gen.hpp:15:54: note: candidate: template<class T, class Mapper> rc::Gen<typename std::decay<typename std::result_of<Mapper(T)>::type>::type> rc::gen::map(rc::Gen<T>, Mapper&&)
 Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
                                                      ^
/usr/local/include/rapidcheck/Gen.hpp:15:54: note:   template argument deduction/substitution failed:
In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h:29:8: note:   candidate expects 2 arguments, 1 provided
       });
        ^
./test/gen/crypto_gen.h: In static member function ‘static rc::Gen<CPubKey> rc::Arbitrary<CPubKey>::arbitrary()’:
./test/gen/crypto_gen.h:39:8: error: no matching function for call to ‘map(rc::Arbitrary<CPubKey>::arbitrary()::<lambda(CKey)>)’
       });
        ^
In file included from /usr/local/include/rapidcheck/Gen.h:74:0,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/Gen.hpp:15:54: note: candidate: template<class T, class Mapper> rc::Gen<typename std::decay<typename std::result_of<Mapper(T)>::type>::type> rc::gen::map(rc::Gen<T>, Mapper&&)
 Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
                                                      ^
/usr/local/include/rapidcheck/Gen.hpp:15:54: note:   template argument deduction/substitution failed:
In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h:39:8: note:   candidate expects 2 arguments, 1 provided
       });
        ^
./test/gen/crypto_gen.h: In static member function ‘static rc::Gen<uint256> rc::Arbitrary<uint256>::arbitrary()’:
./test/gen/crypto_gen.h:49:8: error: no matching function for call to ‘map(rc::Arbitrary<uint256>::arbitrary()::<lambda(int)>)’
       }); 
        ^
In file included from /usr/local/include/rapidcheck/Gen.h:74:0,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/Gen.hpp:15:54: note: candidate: template<class T, class Mapper> rc::Gen<typename std::decay<typename std::result_of<Mapper(T)>::type>::type> rc::gen::map(rc::Gen<T>, Mapper&&)
 Gen<Decay<typename std::result_of<Mapper(T)>::type>> map(Gen<T> gen,
                                                      ^
/usr/local/include/rapidcheck/Gen.hpp:15:54: note:   template argument deduction/substitution failed:
In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h:49:8: note:   candidate expects 2 arguments, 1 provided
       }); 
        ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp: In instantiation of ‘struct rc::Arbitrary<std::vector<unsigned char> >’:
/usr/local/include/rapidcheck/gen/Arbitrary.h:17:33:   required by substitution of ‘template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary() [with T = std::vector<unsigned char>]’
./test/gen/script_gen.h:13:66:   required from here
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:22:62: error: incomplete type ‘rc::gen::detail::DefaultArbitrary<std::vector<unsigned char> >’ used in nested name specifier
   static decltype(gen::detail::DefaultArbitrary<T>::arbitrary()) arbitrary() {
                                                              ^
In file included from ./test/gen/transaction_gen.h:9:0,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/script_gen.h: In static member function ‘static rc::Gen<CScript> rc::Arbitrary<CScript>::arbitrary()’:
./test/gen/script_gen.h:13:66: error: no matching function for call to ‘arbitrary()’
       return gen::map(gen::arbitrary<std::vector<unsigned char>>(), [](std::vector<unsigned char> script) {
                                                                  ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note: candidate: template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary()
 decltype(Arbitrary<T>::arbitrary()) arbitrary() {
                                     ^
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note:   substitution of deduced template arguments resulted in errors seen above
In file included from test/gen/transaction_gen.cpp:8:0:
./test/gen/transaction_gen.h: In static member function ‘static rc::Gen<COutPoint> rc::Arbitrary<COutPoint>::arbitrary()’:
./test/gen/transaction_gen.h:17:23: error: ‘tuple’ is not a member of ‘rc::gen’
       return gen::map(gen::tuple(gen::arbitrary<uint256>(), gen::arbitrary<uint32_t>()), [](std::tuple<uint256, uint32_t> outPointPrimitives) {
                       ^
./test/gen/transaction_gen.h:17:23: note: suggested alternative:
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/memory:62,
                 from /usr/local/include/rapidcheck/Seq.h:4,
                 from /usr/local/include/rapidcheck/Shrinkable.h:3,
                 from /usr/local/include/rapidcheck/Gen.h:4,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:83:11: note:   ‘std::tuple’
     class tuple;
           ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp: In instantiation of ‘struct rc::Arbitrary<unsigned int>’:
/usr/local/include/rapidcheck/gen/Arbitrary.h:17:33:   required by substitution of ‘template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary() [with T = unsigned int]’
./test/gen/transaction_gen.h:17:86:   required from here
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:22:62: error: incomplete type ‘rc::gen::detail::DefaultArbitrary<unsigned int>’ used in nested name specifier
   static decltype(gen::detail::DefaultArbitrary<T>::arbitrary()) arbitrary() {
                                                              ^
In file included from test/gen/transaction_gen.cpp:8:0:
./test/gen/transaction_gen.h:17:86: error: no matching function for call to ‘arbitrary()’
       return gen::map(gen::tuple(gen::arbitrary<uint256>(), gen::arbitrary<uint32_t>()), [](std::tuple<uint256, uint32_t> outPointPrimitives) {
                                                                                      ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note: candidate: template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary()
 decltype(Arbitrary<T>::arbitrary()) arbitrary() {
                                     ^
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note:   substitution of deduced template arguments resulted in errors seen above
In file included from test/gen/transaction_gen.cpp:8:0:
./test/gen/transaction_gen.h: In static member function ‘static rc::Gen<CTxIn> rc::Arbitrary<CTxIn>::arbitrary()’:
./test/gen/transaction_gen.h:30:23: error: ‘tuple’ is not a member of ‘rc::gen’
       return gen::map(gen::tuple(gen::arbitrary<COutPoint>(), gen::arbitrary<CScript>(), gen::arbitrary<uint32_t>()), [](std::tuple<COutPoint, CScript, uint32_t> txInPrimitives) { 
                       ^
./test/gen/transaction_gen.h:30:23: note: suggested alternative:
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/memory:62,
                 from /usr/local/include/rapidcheck/Seq.h:4,
                 from /usr/local/include/rapidcheck/Shrinkable.h:3,
                 from /usr/local/include/rapidcheck/Gen.h:4,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:83:11: note:   ‘std::tuple’
     class tuple;
           ^
In file included from test/gen/transaction_gen.cpp:8:0:
./test/gen/transaction_gen.h:30:115: error: no matching function for call to ‘arbitrary()’
       return gen::map(gen::tuple(gen::arbitrary<COutPoint>(), gen::arbitrary<CScript>(), gen::arbitrary<uint32_t>()), [](std::tuple<COutPoint, CScript, uint32_t> txInPrimitives) { 
                                                                                                                   ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note: candidate: template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary()
 decltype(Arbitrary<T>::arbitrary()) arbitrary() {
                                     ^
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note:   template argument deduction/substitution failed:
In file included from test/gen/transaction_gen.cpp:1:0:
/usr/local/include/rapidcheck/gen/Arbitrary.h: In substitution of ‘template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary() [with T = unsigned int]’:
./test/gen/transaction_gen.h:30:115:   required from here
/usr/local/include/rapidcheck/gen/Arbitrary.h:17:33: error: ‘arbitrary’ is not a member of ‘rc::Arbitrary<unsigned int>’
 decltype(Arbitrary<T>::arbitrary()) arbitrary();
                                 ^
In file included from test/gen/transaction_gen.cpp:8:0:
./test/gen/transaction_gen.h: In static member function ‘static rc::Gen<long int> rc::Arbitrary<long int>::arbitrary()’:
./test/gen/transaction_gen.h:48:14: error: ‘inRange’ is not a member of ‘rc::gen’
       return gen::inRange(std::numeric_limits<int64_t>::min(),std::numeric_limits<int64_t>::max());
              ^
./test/gen/transaction_gen.h: In static member function ‘static rc::Gen<CTxOut> rc::Arbitrary<CTxOut>::arbitrary()’:
./test/gen/transaction_gen.h:55:23: error: ‘tuple’ is not a member of ‘rc::gen’
       return gen::map(gen::tuple(gen::arbitrary<CAmount>(), gen::arbitrary<CScript>()), [](std::tuple<CAmount, CScript> txOutPrimitives) {
                       ^
./test/gen/transaction_gen.h:55:23: note: suggested alternative:
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/5/memory:62,
                 from /usr/local/include/rapidcheck/Seq.h:4,
                 from /usr/local/include/rapidcheck/Shrinkable.h:3,
                 from /usr/local/include/rapidcheck/Gen.h:4,
                 from /usr/local/include/rapidcheck/gen/Arbitrary.h:3,
                 from test/gen/transaction_gen.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:83:11: note:   ‘std::tuple’
     class tuple;
           ^
test/gen/transaction_gen.cpp: In function ‘rc::Gen<std::vector<CTxIn> > rc::oneOrMoreInputs()’:
test/gen/transaction_gen.cpp:14:12: error: ‘suchThat’ is not a member of ‘rc::gen’
     return gen::suchThat(gen::arbitrary<std::vector<CTxIn>>(), [](std::vector<CTxIn> vin) {
            ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp: In instantiation of ‘struct rc::Arbitrary<std::vector<CTxIn> >’:
/usr/local/include/rapidcheck/gen/Arbitrary.h:17:33:   required by substitution of ‘template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary() [with T = std::vector<CTxIn>]’
test/gen/transaction_gen.cpp:14:61:   required from here
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:22:62: error: incomplete type ‘rc::gen::detail::DefaultArbitrary<std::vector<CTxIn> >’ used in nested name specifier
   static decltype(gen::detail::DefaultArbitrary<T>::arbitrary()) arbitrary() {
                                                              ^
test/gen/transaction_gen.cpp:14:61: error: no matching function for call to ‘arbitrary()’
     return gen::suchThat(gen::arbitrary<std::vector<CTxIn>>(), [](std::vector<CTxIn> vin) {
                                                             ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note: candidate: template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary()
 decltype(Arbitrary<T>::arbitrary()) arbitrary() {
                                     ^
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note:   substitution of deduced template arguments resulted in errors seen above
test/gen/transaction_gen.cpp: In function ‘rc::Gen<std::vector<CTxOut> > rc::oneOrMoreOutputs()’:
test/gen/transaction_gen.cpp:21:12: error: ‘suchThat’ is not a member of ‘rc::gen’
     return gen::suchThat(gen::arbitrary<std::vector<CTxOut>>(), [](std::vector<CTxOut> vout) {
            ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp: In instantiation of ‘struct rc::Arbitrary<std::vector<CTxOut> >’:
/usr/local/include/rapidcheck/gen/Arbitrary.h:17:33:   required by substitution of ‘template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary() [with T = std::vector<CTxOut>]’
test/gen/transaction_gen.cpp:21:62:   required from here
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:22:62: error: incomplete type ‘rc::gen::detail::DefaultArbitrary<std::vector<CTxOut> >’ used in nested name specifier
   static decltype(gen::detail::DefaultArbitrary<T>::arbitrary()) arbitrary() {
                                                              ^
test/gen/transaction_gen.cpp:21:62: error: no matching function for call to ‘arbitrary()’
     return gen::suchThat(gen::arbitrary<std::vector<CTxOut>>(), [](std::vector<CTxOut> vout) {
                                                              ^
In file included from /usr/local/include/rapidcheck/gen/Arbitrary.h:22:0,
                 from test/gen/transaction_gen.cpp:1:
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note: candidate: template<class T> decltype (rc::Arbitrary<T>::arbitrary()) rc::gen::arbitrary()
 decltype(Arbitrary<T>::arbitrary()) arbitrary() {
                                     ^
/usr/local/include/rapidcheck/gen/Arbitrary.hpp:13:37: note:   substitution of deduced template arguments resulted in errors seen above
make[1]: *** [test/gen/test_test_bitcoin-transaction_gen.o] Error 1
make[1]: Leaving directory `/home/chris/dev/bitcoin/src'
make: *** [all] Error 2

我真的不确定为什么会发生这种情况。似乎当我包含transaction_gen.h它删除了我包含的所有其他头文件时,这些问题就像

In file included from ./test/gen/script_gen.h:6:0,
                 from ./test/gen/transaction_gen.h:9,
                 from test/gen/transaction_gen.cpp:8:
./test/gen/crypto_gen.h: In static member function ‘static rc::Gen<CKey> rc::Arbitrary<CKey>::arbitrary()’:
./test/gen/crypto_gen.h:19:8: error: no matching function for call to ‘map(rc::Arbitrary<CKey>::arbitrary()::<lambda(int)>)’
       });

因为当我排除transaction_gen.h时,这显然是正确包含的.我哪里出错了?我觉得这是一个非常简单的修复程序,但我现在花了几个小时来使用它:/

编辑:我已将其推送到github

这是transaction_gen.h

这是transaction_gen.cpp

尝试从transaction_gen.cpp中删除以下行:

#include <rapidcheck/gen/Arbitrary.h>
#include <rapidcheck/Gen.h>
#include "primitives/transaction.h" 
#include "script/script.h"
#include "amount.h"

仅保留此 #include 行:

#include "test/gen/transaction_gen.h"
您的transaction_gen.h文件

末尾缺少一个#endif

将这些也包含在您的 cpp 文件中

#include "test/gen/script_gen.h"
#include "test/gen/crypto_gen.h"