Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+;中使用if条件仅声明一个变量(模板)+;_C++_Templates - Fatal编程技术网

C++ 在C+;中使用if条件仅声明一个变量(模板)+;

C++ 在C+;中使用if条件仅声明一个变量(模板)+;,c++,templates,C++,Templates,我想使用如下所示的if条件创建模板类“stack”的单个对象,并在if条件之外使用它 template <class TYPE> class stack{ .. .. }; main() { int opt; cin>>opt; class stackBase{ public: virtual void push() = 0; //pure virtual function virtual void pop() = 0; /

我想使用如下所示的if条件创建模板类“stack”的单个对象,并在if条件之外使用它

template <class TYPE>

class stack{
..
..
};
main()
{
int opt;
cin>>opt;
class stackBase{
    public:

    virtual void push()    = 0;  //pure virtual function
    virtual void pop()     = 0;  //pure virtual function   
    virtual void display() = 0;  //pure virtual function   
};

template <typename T>
class stackDerived : public stackBase{

    T data[100];
    int top;
public:
    void push()
    {
    T ele;
    cin>>ele;
    }
//definitions of pop and display are also included in this class
};

int main()
{
stackBase *s;
if(condition)
    s = new stackDerived<int>;
else
    s = new stackDerived<char>;

within switch case
s->push(integer or character as required);
}
模板
类堆栈{
..
..
};
main()
{
int-opt;
cin>>opt;
这里是if条件

if(opt == 1)
    stack<int> s;
else
    stack<float> s;

s.push(1);
}
if(opt==1)
堆栈s;
其他的
堆栈s;
s、 推(1);
}

我面临的问题是对象“s”是在块范围内创建的,我无法在if条件之外访问该对象。

这非常简单。只需将通用代码放入函数模板中

template <class TYPE>

class stack{
..
..
};
main()
{
int opt;
cin>>opt;
template< class Number >
void foo()
{
    stack<Number> s;
    s.push( 1 );
}

auto main() -> int
{
    int opt;
    cin>>opt;
    opt == 1? foo<int>() : foo<double>();
}
class stackBase{
    public:

    virtual void push()    = 0;  //pure virtual function
    virtual void pop()     = 0;  //pure virtual function   
    virtual void display() = 0;  //pure virtual function   
};

template <typename T>
class stackDerived : public stackBase{

    T data[100];
    int top;
public:
    void push()
    {
    T ele;
    cin>>ele;
    }
//definitions of pop and display are also included in this class
};

int main()
{
stackBase *s;
if(condition)
    s = new stackDerived<int>;
else
    s = new stackDerived<char>;

within switch case
s->push(integer or character as required);
}
模板
void foo()
{
堆栈s;
s、 推(1);
}
auto main()->int
{
int-opt;
cin>>opt;
opt==1?foo():foo();
}

运行前必须知道
s
的类型,但您可以使用如下界面:

class stackI{};

template <class TYPE>
class stack : public stackI {
    std::vector<TYPE> values;
    /*...*/
};

int main(int argc, char *argv[]) {
    stackI* s;
    if (1==0){
        s = new stack<int>;
    } else {
        s = new stack<double>;
    }
}  
类stackI{};
样板
类堆栈:publicstacki{
std::向量值;
/*...*/
};
int main(int argc,char*argv[]){
stackI*s;
如果(1==0){
s=新堆栈;
}否则{
s=新堆栈;
}
}  

我终于找到了答案。 在基类中将函数设置为“纯虚拟”。 在派生类中定义纯虚函数。 在main()中声明基类型指针*s, 然后

在if条件内,s=新堆栈派生的
s=新堆栈派生的

template <class TYPE>

class stack{
..
..
};
main()
{
int opt;
cin>>opt;
class stackBase{
    public:

    virtual void push()    = 0;  //pure virtual function
    virtual void pop()     = 0;  //pure virtual function   
    virtual void display() = 0;  //pure virtual function   
};

template <typename T>
class stackDerived : public stackBase{

    T data[100];
    int top;
public:
    void push()
    {
    T ele;
    cin>>ele;
    }
//definitions of pop and display are also included in this class
};

int main()
{
stackBase *s;
if(condition)
    s = new stackDerived<int>;
else
    s = new stackDerived<char>;

within switch case
s->push(integer or character as required);
}
类堆栈库{
公众:
virtual void push()=0;//纯虚函数
虚拟void pop()=0;//纯虚拟函数
虚空显示()=0;//纯虚函数
};
样板
类stack派生:公共stackBase{
T数据[100];
int top;
公众:
无效推送()
{
T元素;
cin>>ele;
}
//pop和display的定义也包含在这个类中
};
int main()
{
stackBase*s;
如果(条件)
s=新的堆栈;
其他的
s=新的堆栈;
开关箱内
s->push(需要整数或字符);
}

问题是什么?查阅SFINAE。这不能用
if()
来完成。我想用if条件声明单个变量“s”,并在if条件之外使用它。@CHAITANYA好吧,你不能这样做,因为你希望变量的类型取决于运行时条件。这不能在C++中完成(但请看<代码> Boo::变体< /代码>不同的方法)。stack_tmp1.cpp:(.text._ZN6stackIC2Ev[_ZN6stackIC5Ev]+0x8):对'vtable for stackI'@CHAITANYA的未定义引用嗯,我必须承认,接口不是很有用,因为如果不强制转换,就不能调用
s
上的任何方法。我正在将基类中的函数(push,pop)设置为虚拟函数。当我把它变成纯虚拟的时候,问题就解决了。