gpt4 book ai didi

c++ - 编译器生成的错误方法调用

转载 作者:太空狗 更新时间:2023-10-29 23:05:58 25 4
gpt4 key购买 nike

window 7Cygwin 1.7.20海合会 4.5.3

我的一个类中有一个方法,enqueue(const PTR X, const void* operation),当尝试调用它时,实际上调用了另一个方法,enqueue(bool X)' 当原始方法改为,enqueue(const PTR X) 调用正确。我无法弄清楚我可能做了什么来激起这种行为。代码是可编译的。

/****** SlipDef.h ******/

#ifndef SLIPDEF_H
#define SLIPDEF_H
# include <string>
# include "SlipPointer.h"
using namespace std;
namespace slip {
typedef unsigned char UCHAR;
typedef signed char CHAR;
typedef unsigned short USHORT;
typedef signed short SHORT;
typedef unsigned int UINT;
typedef signed int INT;
typedef unsigned long ULONG;
typedef signed long LONG;
typedef float FLOAT;
typedef double DOUBLE;
typedef SlipPointer * PTR;
typedef SlipPointer * STRING;
} // namespace slip
#endif /* SLIPDEF_H */
/****** SlipHeader.h ******/

#ifndef _SLIPHEADER_H
#define _SLIPHEADER_H
# include <string>
# include "SlipDef.h"
namespace slip {

class SlipHeader {
public:
SlipHeader& enqueue(SlipHeader& X);
SlipHeader& enqueue(bool X);
SlipHeader& enqueue(UCHAR X);
SlipHeader& enqueue(CHAR X);
SlipHeader& enqueue(ULONG X);
SlipHeader& enqueue(LONG X);
SlipHeader& enqueue(DOUBLE X);
SlipHeader& enqueue(const PTR X, const void* operation);
SlipHeader& enqueue(const string& X, bool constFlag = false);
SlipHeader& enqueue(const string* X, bool constFlag = false);
};
};
#endif /* SLIPHEADER_H */

/****** SlipPointer.h ******/

#ifndef SLIPPOINTER_H
#define SLIPPOINTER_H
using namespace std;
namespace slip {

class SlipPointer {
public:
};
} // namespace slip
#endif /* SLIPPOINTER_H */

/****** TheOtherHeader.h ******/

#ifndef _THEOTHERHEADER_H
#define _THEOTHERHEADER_H
# include <string>
# include "SlipDef.h"
namespace slip {

class TheOtherHeader {
public:
TheOtherHeader& enqueue(TheOtherHeader& X);
TheOtherHeader& enqueue(bool X);
TheOtherHeader& enqueue(UCHAR X);
TheOtherHeader& enqueue(CHAR X);
TheOtherHeader& enqueue(ULONG X);
TheOtherHeader& enqueue(LONG X);
TheOtherHeader& enqueue(DOUBLE X);
TheOtherHeader& enqueue(const PTR X);
TheOtherHeader& enqueue(const string& X, bool constFlag = false);
TheOtherHeader& enqueue(const string* X, bool constFlag = false);
};
};
#endif /* _THEOTHERHEADER_H */

/****** main.cpp ******/

# include "SlipHeader.h"
#include "TheOtherHeader.h"
# include <string>
# include <iostream>
using namespace std;
using namespace slip;

int main(int argc, char** argv) {
SlipPointer* ptr = new SlipPointer();
SlipHeader* header = new SlipHeader();
TheOtherHeader* theOtherHeader = new TheOtherHeader();
header->enqueue(ptr);
theOtherHeader->enqueue(ptr);
return 0;
}

/****** SlipHeader.cpp ******/

# include <string>
# include <iostream>
# include "SlipDef.h"
# include "SlipHeader.h"
namespace slip {
static void* ptrOP;
SlipHeader& SlipHeader::enqueue(SlipHeader& X) { cout << "enqueue(SlipHeader& X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(bool X) { cout << "enqueue(bool X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(UCHAR X) { cout << "enqueue(UCHAR X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(CHAR X) { cout << "enqueue(CHAR X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(ULONG X) { cout << "enqueue(ULONG X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(LONG X) { cout << "enqueue(LONG X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(DOUBLE X) { cout << "enqueue(DOUBLE X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(const PTR X, const void* operation = ptrOP) {
cout << "enqueue(PTR X) right answer" << endl; return *new SlipHeader();
};
SlipHeader& SlipHeader::enqueue(const string& X, bool constFlag) { cout << "enqueue(string& X) wrong answer" << endl; return *new SlipHeader();}
SlipHeader& SlipHeader::enqueue(const string* X, bool constFlag) { cout << "enqueue(string* X) wrong answer" << endl; return *new SlipHeader();}
};
/****** TheOtherHeader.cpp ******/

# include <string>
# include <iostream>
# include "SlipDef.h"
# include "TheOtherHeader.h"
namespace slip {
static void* ptrOP;
TheOtherHeader& TheOtherHeader::enqueue(TheOtherHeader& X) { cout << "enqueue(TheOtherHeader& X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(bool X) { cout << "enqueue(bool X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(UCHAR X) { cout << "enqueue(UCHAR X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(CHAR X) { cout << "enqueue(CHAR X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(ULONG X) { cout << "enqueue(ULONG X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(LONG X) { cout << "enqueue(LONG X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(DOUBLE X) { cout << "enqueue(DOUBLE X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(const PTR X) {
cout << "enqueue(PTR X) right answer" << endl; return *new TheOtherHeader();
};
TheOtherHeader& TheOtherHeader::enqueue(const string& X, bool constFlag) { cout << "enqueue(string& X) wrong answer" << endl; return *new TheOtherHeader();}
TheOtherHeader& TheOtherHeader::enqueue(const string* X, bool constFlag) { cout << "enqueue(string* X) wrong answer" << endl; return *new TheOtherHeader();}
};

最佳答案

const void* operation 有默认参数。但是,您在实现中而不是 header 中设置这些默认参数 - 应该是相反的方式:

// header
void f(int x=5);

// implementation
void f(int x) { }

原因很简单,如果在主代码中调用header->enqueue(ptr),编译器只知道header给出的信息。如果 header 不包含默认参数,则编译器此时不知道默认参数的存在。

关于c++ - 编译器生成的错误方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17284549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com