我正在阅读的C++0x常见问题,却被这段代码卡住了。考虑以下代码
struct A
{
void f(double)
{
std::cout << "in double" << std::endl;
}
};
struct B : A
{
void f(int)
{
std::cout << "in int" << std::endl;
}
};
int main()
{
A a; a.f(10.10); // as e
此代码生成以下编译错误:
错误:没有调用'C::print(int)‘的匹配函数
你能帮我弄清楚编译器生成错误的过程吗?(为什么它忽略了B类中的函数)
#include <iostream>
using std::cout;
class A {
public:
virtual void print ()
{ cout << "A";}
};
class B : public A {
int x;
virtual void print (int y)
{cout << x+y;}
};
class
我有以下代码:
#include <fstream>
// Removing this namespace (keeping the content) makes it work
namespace baz {
class Bar {
};
}
std::ostream & operator<<(std::ostream & stream, baz::Bar & value) {
return stream;
}
// Removing this namespace (keeping the content) makes it wo
我正在尝试编写类类型,并出现表达我想要的内容的问题。
我有过
class type event_emitter = object
method on : string -> unit
end
然后我想做这个:
class type socket = object
inherit event_emitter
method on : int -> unit
end
但是我得到了一个编译器错误,关于两者之间的类型签名不匹配。我还尝试使用virtual关键字执行class type virtual event_emitter = method virtual on :
我对这两个术语有点困惑,很高兴能澄清一些疑问。
据我所知,function overloading意味着在同一个类中有多个方法具有相同的名称,但参数的数量不同,参数的类型不同,或者参数的序列与返回类型无关,这对损坏的函数名称没有任何影响。
上述定义是否也包括"....in同一类或跨相关类(通过继承相关).....“
Function Overriding与虚函数、相同的方法签名(在基类中声明为虚)以及在子类中的实现被重写有关。
我在想一个场景,以下是代码:
#include <iostream>
class A
{
public:
void doSomet
在下面的示例中,为什么我必须显式地使用b->A::DoSomething()语句,而不只是b->DoSomething()语句
编译器的重载解析不应该知道我说的是哪种方法吗?
我使用的是Microsoft VS 2005。(注意:在这种情况下,使用虚拟不会有什么帮助。)
class A
{
public:
int DoSomething() {return 0;};
};
class B : public A
{
public:
int DoSomething(int x) {return 1;};
};
int main()
{
B* b = ne
在处理ocaml项目时,我发现了一些我不太理解的东西。
假设我同时使用了OCaml标准库的Array和List模块。它们都实现了函数length,但具有不同的类型。在List模块中,其类型如下:
length: a' list -> int
在Array模块中,它的类型是:
length: a' array -> int
但是,我希望您通过open关键字在我实现的同一个模块中使用这两个模块:
open List
open Array
当我试图在列表上使用length函数时,我在编译过程中遇到了类型错误。
由于OCaml是一种强静态类型的语言,我想知道为什么编译器不知
描述
我正在重写<< operator以使std::ostream易于在代码中显示对象。在定义要显示的对象类型时,我使用不同的命名空间。
这导致了编译错误。我找到解决办法了。似乎必须在全球范围内声明压倒一切,但我真的不明白为什么。有人能解释一下是什么原因造成的错误吗?
错误
main.cpp:22:38: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘std::vector’)
std::cout <<"printVecto
我有两个具有相同名称和相同参数类型的方法。我想重载这些,但它不起作用,因为我得到了这个错误:
"<Class-name> already defines a member called 'ChangeProfileInformation'
with the same parameter types"
我有这样的东西:
public void ChangeProfileInformation(User user)
{
a
b
}
public void ChangeProfileInformation(User user)
{
def func(a, b, c='four'):
print 'a is %s, b is %s, c is %s' %(a, b ,c)
func('one','two','three')
func('one', 'two')
此代码运行时没有任何问题。但这叫什么呢?“重载”?
顺便说一句,这种风格只在Python中可用吗?谢谢!
如果需要在派生类中使用这些运算符,必须重新定义具有派生类型的所有重载运算符吗?
以下代码编译得很好:
class Point {
public:
Point(int X = 0, int Y = 0):x(X), y(Y) {}
virtual ~Point() {}
Point operator +(Point &rhs) {
return Point(x + rhs.x, y + rhs.y);
}
protected:
int x, y;
};
class Vector : public Point {
pub
面试官问我什么是方法重载,我给出的答案是,方法重载是在同一个类中,其中两个或多个方法具有相同的名称但不同的签名,示例如下:
//method with two parameters
public int add(int a, int b){
return a + b;
}
//method with three parameters
public int add(int a, int b, int c){
return a + b + c;
}
对于这个例子,他说为什么我们需要两个同名的方法,即add。相反,我们可以有两个不同名称的方法,即addTwoNumbers和addT