这不起作用:
class Foo
{
public:
virtual int A(int);
virtual int A(int,int);
};
class Bar : public Foo
{
public:
virtual int A(int);
};
Bar b;
int main()
{
b.A(0,0);
}
Run Code Online (Sandbox Code Playgroud)
看来,通过重写Foo::A(int)与Bar::A(int)我莫名其妙地隐藏Foo::A(int,int).如果我添加一些Bar::A(int,int)东西工作.
有没有人能够很好地描述这里发生的事情?
我对以下代码的编译器错误感到困惑:
class Base {
public:
virtual ~Base () { }
virtual void func () { }
virtual void func (int) { }
virtual void another () { }
virtual void another (int) { }
};
class Derived : public Base {
public:
void func () { }
};
int main () {
Derived d;
d.func();
d.func(0);
d.another();
d.another(0);
}
Run Code Online (Sandbox Code Playgroud)
使用gcc 4.6.3,上面的代码在d.func(0)处导致错误,说Dervied :: func(int)是未定义的.
当我还为Derived添加func(int)的定义时,它就可以了.当我在Derived中既未定义func()也未定义func(int)时,它也有效(如"另一个"的情况).
显然这里有一些关于虚拟重载函数的规则,但这是我第一次遇到它,我不太明白它.有人能告诉我这到底发生了什么吗?
我无法使派生类访问基类中定义的函数.基类头文件名为Particle.h,它是:
class Particle {
protected:
vector<double> x_,y_,z_;
// lots of other protected members that are not relevant to the question
public:
// constructors and other functions not relevant to question are omitted
virtual double getX(const unsigned int index, const double theta, const double phi);
virtual vector<double> getX(const double theta, double Real phi);
vector<double> getX(const unsigned int surfindex);
}
Run Code Online (Sandbox Code Playgroud)
该函数的定义位于名为Particle.cc的文件中:
#include "Particle.h"
vector<double> Particle::getX(const unsigned int surfindex)
{
vector<double> xp;
xp.clear();
xp.resize(3,0.0);
xp[0] = x_.at(surfindex);
xp[1] = y_.at(surfindex);
xp[2] = z_.at(surfindex); …Run Code Online (Sandbox Code Playgroud) 有谁知道为什么这段代码无法在visual studio 2013中编译?问题在于ba()只有一个版本(B类a(float)中的覆盖版本)和版本a(std :: string)不可用,尽管它在基类中.
#include <string>
template <typename T>
class A {
public:
virtual void a(std::string b){ this->a(123); }
virtual void a(float b) = 0;
};
class B : public A < std::string > {
public:
virtual void a(float b) override {}
};
main()
{
B b;
b.a(""); // Error here: error C2664:
// 'void B::a(float)' : cannot convert argument 1 from 'const char [1]' to 'float'
B* bb = new B();
bb->a(""); // same
}
Run Code Online (Sandbox Code Playgroud) 我一直认为基类的公共方法确实被派生类继承,甚至认为派生类没有定义该特定方法.例如
#include <iostream>
using namespace std;
class A {
public:
int f() { cout << 3; return 0;}
int f(int x) {cout << x; return 0;}
};
class B: public A {
public:
int f() {std::cout << 5; return 0;}
};
int main(){
B ob;
ob.f(7);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我期待结果是:7,但我得到编译错误说
" 错误:函数调用的参数太多,预期为0,有1;你的意思是'A :: f'吗? "
我知道错误试图说的是什么,但我很困惑,没有调用Base类的功能.
我假设这是“只是不是如何工作”的问题之一,但我不明白为什么。为什么需要有资格B的号召,A小号Start用A::。如果我更改B::Start()为,B::DoSomethingElse()我可以在Start()没有A::. 那么发生了什么?
#include <iostream>
#include <string>
class A {
public:
void Start(){
}
};
class B : public A {
public:
void Start(int x){
Start(); // cannot call this
A::Start(); // can call this
}
};
Run Code Online (Sandbox Code Playgroud) 我不知道为什么我得到一个"错误C2660:'SubClass :: Data':函数不带2个参数".当我尝试编译我的项目时.
我有一个基类,其中包含一个名为data的函数.该函数接受一个参数,数据重载需要2个参数.在我的subClass中,我覆盖了带有1个参数的数据函数.现在,当我尝试从指向subClass的指针调用数据的重载时,我收到上面的编译错误.
class Base : public CDocument
{
Public:
virtual CString& Data( UINT index);
CString Data( UINT index, int pos);
};
class SubClass : public Base
{
Public:
virtual CString& Data( UINT index);
};
Void SomeOtherFunction()
{
subType* test = new subType();
test->Data( 1, 1);// will not compile
((Base*)test)->Data(1,1); // compiles with fine.
}
Run Code Online (Sandbox Code Playgroud) 我有两节课:
template<typename T>
class A{
public:
T& someMethod(std::string);
}
template<typename T>
class B: public A<T>{
public:
T& someMethod(T&,T&);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是知道我不能打电话
B b;
b.someMethod("HelloWorld");
Run Code Online (Sandbox Code Playgroud)
因为我的编译器看不到someMethod(std::string).你知道为什么会这样吗?
以免考虑以下代码:
#include <iostream>
template <typename T_VAL> // not used template argument
struct Foo { int x; };
template <typename T_VAL>
struct Bar { int x; };
template <typename T_VALUE>
struct A
{
// just 2 overloaded data() members:
virtual void data(Foo<T_VALUE>) {
std::cout << "FOO EMPTY\n";
}
virtual void data(Bar<T_VALUE>) {
std::cout << "BAR EMPTY\n";
}
};
template <typename T_VALUE>
struct B : public A<T_VALUE>
{
void data(Foo<T_VALUE>) {
std::cout << "smart FOO impl\n";
}
};
int main(void) {
B<float> …Run Code Online (Sandbox Code Playgroud) 为什么编译器找不到基类函数签名?更改foo( a1 )到B::foo( a1 )的作品.
码:
class A1 ;
class A2 ;
class B
{
public:
void foo( A1* a1 ) { a1 = 0 ; }
} ;
class C : public B
{
public:
void foo( A2* /*a2*/ )
{
A1* a1 = 0 ;
foo( a1 ) ;
}
} ;
int main()
{
A2* a2 = 0 ;
C c ;
c.foo( a2 ) ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
编译器错误(VS2008):
error C2664: …Run Code Online (Sandbox Code Playgroud) c++ ×10
inheritance ×6
class ×2
overloading ×2
overriding ×2
templates ×2
g++ ×1
gcc ×1
name-lookup ×1
polymorphism ×1
scope ×1
virtual ×1
visual-c++ ×1