Reputation: 1897
I am trying to compile the following code using VS2010:
typedef enum EPinDirection{
EPinDirection_Unknown,
EPinDirection_In,
EPinDirection_Out
};
class cPin{
std::tuple<QString,EPinDirection> m_nameDir;
public:
cPin(){ m_nameDir = std::tuple<QString, EPinDirection>("noNamePin", EPinDirection_Unknown);}
cPin(QString name, EPinDirection dir) { m_nameDir = std::tuple<QString, EPinDirection>(name, dir); }
const std::tuple<QString,EPinDirection>& getNameDir() const {return m_nameDir;};
};
class cConnection{
std::tuple<const cPin&,const cPin&> m_data;
public :
cConnection();
cConnection(const cPin& start, const cPin& stop) {m_data = std::tuple<const cPin&, const cPin&>(start, stop);}
int setData(const cPin& start, const cPin& stop) {m_data = std::tuple<const cPin&, const cPin&>(start, stop);}
const std::tuple<const cPin&,const cPin&> & getData() const {return m_data;}
};
The compilation fails with message: cannot convert from 'int' to 'const cPin &'. The indicated line number is at the declaration of the tuple in the cConnection.
I have no idea why. What could be the cause?
Upvotes: 0
Views: 540
Reputation: 30418
The compile error is due to the compiler attempting to initialize cConnection::m_data
with a default value. Since the tuple is expecting two const references, it can't do this and therefore the error is generated.
You can fix this by removing the default constructor for cConnection
, and changing the remaining constructor to use an initializer:
class cConnection {
std::tuple<const cPin&, const cPin&> m_data;
public:
cConnection(const cPin& start, const cPin& stop)
: m_data(std::tuple<const cPin&, const cPin&>(start, stop)) {
}
}
(You could also fix this by changing the tuple to have pointers rather than references, but that might not make sense in the context of your program.)
If you would like to simplify the code further, consider using std::make_tuple
(which can infer the type arugments) rather than the tuple
constructor:
class cConnection {
std::tuple<const cPin&, const cPin&> m_data;
public:
cConnection(const cPin& start, const cPin& stop)
: m_data(std::make_tuple(start, stop)) {
}
}
Upvotes: 2