我尝试使用clang (3.0版)编译以下代码,但它给出了这个错误
error: no matching member function for call to 'a'
在调用__a.a<0>()中。然后,我尝试使用g++ (版本4.2.1),它可以按照预期进行编译和工作(打印出1 2)。
#include <iostream>
struct A {
template <int> int a() { return 1; }
};
struct B: A {
using A::a;
template <int,int>
--一个非常简单但又令人困惑的问题:
为什么使用指令会改变继承!?这是用Comeau编译的。
我读过一个使用指令(解密?)使变量公开,但为什么?我想要的只是一个很好的方法,不要总是在B里面写this->x .?
class A{
protected:
int x;
public:
};
class B: public A {
public:
using A::x;
};
int main(){
B b;
b.x = 2;
}
谢谢!
我在考虑使用CRTP类来帮助处理重载,并且想知道下面的代码会做什么:
#include <iostream>
#include <typeinfo>
template <class TDerived>
class FunctionImplementation
{
};
class AbstractFunction
{
};
class BaseFunction : public AbstractFunction, public FunctionImplementation<BaseFunction>
{
};
class DerivedF
在问我的问题之前,我试着在网上搜索,但我不确定我需要使用什么术语,我也没有找到对我的问题的解释。如果这样的答案已经存在,请随时告诉我:)
下面是一个小示例:
class IObject;
class ClassA;
class Base {
public: void Method(IObject * object) {}
};
class Derived : public Base {
public: void Method(ClassA * objet) {}
};
class IObject {};
class ClassA : public IObject {};
int mai
考虑一下C++中的以下情况:
template<int n>
class Base { ... };
class Derived3 : public Base<3> {
// a complicated body, making use of n=3
};
class Derived7 : public Base<7> {
// a completely different body, making use of n=7
};
在Derived3成员函数中,我希望显式地使用n=3,而在Derived7、n=7中,不对数字进行硬编码,即仍然引用类似
我有一个抽象类,如下所示:
abstract class AbstractFoo implements Bar {
//Code goes here
}
然而,当我试图保护AbstractFoo时,我得到了一个错误,编译时错误,抱怨它是一个非法的修饰符。
protected abstract class AbstractFoo implements Bar {
//Code goes here
}
为什么在Java中不能有一个受保护的抽象类呢?
编辑:我应该提一下,这不是香草Java,实际上是黑莓/ J2ME。
为什么我得到下面这段代码的访问说明符错误(私有成员)?
#include<iostream>
using namespace std;
class Derived;
class Base {
private:
virtual void fun() { cout << "Base Fun"; }
};
class Derived: public Base {
public:
void fun() { cout << "Derived Fun"; } //this should be called
};
i
关于会员指针我有个问题。以下代码无法使用12.2的CC和cygwin 4.3.4进行编译,但使用的是Microsoft C++ 2010:
struct A {
int x;
};
struct B : public A {
};
template<typename T> class Bar {
public:
template<typename M> void foo(M T::*p);
};
int main(int, char *[]) {
Bar<B> bbar;
bbar.foo(&B::x);
ret
我在vc++有一个类似下面这样的c++类,但是在Linux4.7中就不能工作了。我不知道怎么才能让它再次工作。
test.h
template<typename a>
class test: public a
{
public:
void fun();
};
test.cpp
template<typename a>
void test<a>::fun()
{
template_class_method(); <-- this is a public method from template_class
}
template c
我是C++的新手。在我的学习阶段,我遇到了以下问题。我正在尝试从一个类模板Queue派生一个类stack。编译器在stack的构造函数中引发以下错误
..\src\TrainingDay2.cpp:44:3: error: 'b' was not declared in this scope
b=a;
请帮助找出根本原因。
#include <iostream>
using std::cout;
using std::endl;
template<class T> class Queue //Base class
{
private:
T
I希望对各种模板类的每种类型都有一个函数重载。有可能吗?
template<typename ...args>
class Example {
virtual void doSomething(args(0) arg) { ... }
virtual void doSomething(args(1) arg) { ... }
/// etc... implementations are the same, but I need access to the type
}
我试过用折叠表达,但我很确定我没有走上正确的轨道。因为我需要函数是虚拟的,所以我不能将它
我编写了以下隐藏成员函数模板的代码。
#include <iostream>
struct A
{
template<int> void func()
{
std::cout<<"Function tamplate of A"<<std::endl;
}
};
struct B : A
{
template<char> void func()
{
std::cout<<"Function tamplate of B
我希望允许通过指定策略来修改我们类的行为。这一政策应该被用作boost::variant的访问者。有些默认策略适合大多数情况,但用户可能需要添加或替换一些重载。
我发现vc++ 2013没有用错误C3066: there are multiple ways that an object of this type can be called with these arguments编译这段代码。在gcc和clang中,相同的代码编译和工作。
是vc++ 2013的错误吗?
#include <iostream>
struct DefaultPolicy
{
void ope
如果这个问题看上去含糊不清或令人困惑,我道歉。这是特尔斐棱镜.NET。
我有一个基类,其变量称为矩形类型的界。从这个类中,另一个类派生或继承并访问基类变量边界。在设计时,编译器从基类中识别边界变量,但在调试期间,它会不断为基类中的变量边界引发未知错误。因此,我的程序编译成功,但无法正确运行。
下面是基类和变量:
TControlObject = public class
bounds:Rectangle; <<=========This is the Variable in question
private
protected
public
en
我使用MSVC++编译器。当我做这样的事情时,我不会犯任何错误:
class Base {
protected:
Base() {}
public:
void f() {}
};
class Child: public Base {
public:
Child(): Base() {}
};
这是正确的代码还是有MSVC++编译器的特性?
我有这个代码,和GCC 9.1的预期一样:
#include <type_traits>
template< typename T >
class A
{
protected:
T value;
public:
template< typename U,
typename...,
typename = std::enable_if_t< std::is_fundamental< U >::value > >
A& operator=(U v)
我的代码中有一个函数,它只接受一个类成员方法作为模板参数。我需要使用从父类继承的类方法来调用此方法。下面是我的问题的一个示例代码:
template <class C>
class Test {
public:
template<typename R, R( C::* TMethod )()> // only a member function should be accepted here
void test() {}
};
class A {
public:
int a() { return 0; } // dummy metho
我很难理解为什么下面两段代码会有差异,编译器到底在做什么。
我有以下一点简单的代码,编译时没有任何问题,如预期的:
class base
{
public:
typedef int booboo;
};
class derived : public base
{
public:
int boo()
{
booboo bb = 1;
return bb;
}
};
int main()
{
derived d;
d.boo();
return 0;
}
我从上面的代码中获取并添加了一些模板参数,并且开始得到与类型booboo无