我正在使用gcc 4.3.2。轉換問題
我有以下代碼(簡化):
#include <cstdlib>
template<int SIZE>
class Buffer
{
public:
explicit Buffer(const char *p = NULL) {}
explicit Buffer(const Buffer &other);
const char *c_str() const { return m_buffer; }
private:
char m_buffer[SIZE];
};
typedef Buffer<10> A;
typedef Buffer<20> B;
void Foo(A a) {
}
int main()
{
B b;
Foo(b.c_str()); // line 25 fails compilation
return 1;
}
編譯收率:
test.cpp: In function ‘int main()’:
test.cpp:25: error: conversion from ‘const char*’ to non-scalar type ‘A’ requested
但是有C-TOR接收常量字符*。
UDP:
如果我從第一個C-TOR刪除明確我收到
test.cpp: In function ‘int main()’:
test.cpp:25: error: no matching function for call to ‘Buffer<10>::Buffer(A)’
test.cpp:7: note: candidates are: Buffer<SIZE>::Buffer(const char*) [with int SIZE = 10]
test.cpp:25: error: initializing argument 1 of ‘void Foo(A)’
如果我使用美孚(A(b.c_str()))我得到:
test.cpp: In function ‘int main()’:
test.cpp:25: error: no matching function for call to ‘Buffer<10>::Buffer(A)’
test.cpp:25: error: initializing argument 1 of ‘void Foo(A)’
我發佈了一個類似的問題,但在一個更簡單的緊急情況下,有相同的問題,沒有「明確的」分心。 http://stackoverflow.com/questions/7735107/how-do-i-fix-a-const-char-constructor-conversion-chain-error-in-gcc-compile – peterk