![](/img/trans.png)
[英]Including a precompiled header and a non-precompiled header in a .cpp file causes the .cpp file to not recognize the non-precompiled header
[英]Including header file in .cpp causes nasty error
我有一個叫做頭文件transaction_gen.h
,並要求相應的cpp文件transaction_gen.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.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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.