注册 登录
编程论坛 C++教室

救救孩子吧

牛雅杰 发布于 2021-08-03 20:35, 3101 次点击
实在不知道怎么回事,就是不能实现不输类型运行,模板类型不是可以自动识别吗?
程序代码:

#include <iostream>
using namespace std;
template<class T>
class sring{
    public:
    T data;
    constexpr sring(T d):data(d){}
};

int main(){
    sring a=1;
    cout<<a.data;
}

结果是这样,gcc,windows上
程序代码:

c:\c++>g++ shiyan.cpp
shiyan.cpp: In function 'int main()':
shiyan.cpp:18:11: error: missing template arguments before 'a'
     sring a=1;
           ^
shiyan.cpp:19:11: error: 'a' was not declared in this scope
     cout<<a.data;
2 回复
#2
rjsp2021-08-03 23:12
两个方法,
要么改 sring a=1; 为 sring<int> a=1;
要么换个起码能支持C++17的编译器。
#3
rjsp2021-08-03 23:17
不知道你的gcc版本是多少,加个编译参数吧
g++ -std=c++17
如果你的gcc不支持,那就升级一下gcc
1