如何声明放在main之后的类模板?(How do I declare a class template that is placed after the main?)
我正在尝试实现一个堆栈模板类。 但我想在主要之前宣布它。 我能这样做吗? 我知道如果你把模型放在模板之后它会编译但是有可能先将主要的模板放入模板吗?
#include <iostream> //start declaration of template template <class T> class stack<T>; //end declaration of template int main() { stack<char> s(5); s.push('a'); s.push('b'); cout <<s.pop()<<endl; stack<double> s1(10); s1.push(3.2); s1.push(0.5); cout << s1.pop()<<endl; return 0; } template <class T> class stack { T *s; int size; //How many elements I can stole. int top; //Where the index is available. public: stack(int sz) { size = sz; s = new T[sz]; top=-1; } void push(T e); T pop() { return s[top--]; } }; template <class T> void stack<T>::push(T e) { s[++top] = e; }
错误给出:
1>.\classTemplPers.cpp(6) : error C2143: syntax error : missing ';' before '<' 1>.\classTemplPers.cpp(6) : error C2059: syntax error : '<'
为了记录,这样做:
template <class T> class stack;
也不起作用。 我看到这样的错误:
1>.\classTemplPers.cpp(10) : error C2079: 's' uses undefined class 'stack<T>'
I am trying to implement a template class that is stack. But I want to declare it before main. Can I do that? I know it will compile if you put the main after the template but is it possible to put the main first then the template?
#include <iostream> //start declaration of template template <class T> class stack<T>; //end declaration of template int main() { stack<char> s(5); s.push('a'); s.push('b'); cout <<s.pop()<<endl; stack<double> s1(10); s1.push(3.2); s1.push(0.5); cout << s1.pop()<<endl; return 0; } template <class T> class stack { T *s; int size; //How many elements I can stole. int top; //Where the index is available. public: stack(int sz) { size = sz; s = new T[sz]; top=-1; } void push(T e); T pop() { return s[top--]; } }; template <class T> void stack<T>::push(T e) { s[++top] = e; }
Error given:
1>.\classTemplPers.cpp(6) : error C2143: syntax error : missing ';' before '<' 1>.\classTemplPers.cpp(6) : error C2059: syntax error : '<'
For the record, doing this:
template <class T> class stack;
doesn't work either. I see errors like this:
1>.\classTemplPers.cpp(10) : error C2079: 's' uses undefined class 'stack<T>'
原文:https://stackoverflow.com/questions/36713412
更新时间:2024-05-04 21:05
最满意答案
我是从未经测试的手机上做到的,但你会知道发生了什么
if(isset($check)) { $sql="SELECT * FROM dtr WHERE userID = $userID"; $result = mysqli_query($con,$sql); $check = mysqli_fetch_array($result); if(isset($check)) { echo "Already in"; if(isset($check['timeIN']) && !isset($check['timeOUT'])) { $sql = "UPDATE dtr SET timeOUT= now() WHERE userID=$userID"; mysqli_query($con, $sql); mysqli_close($con); } } else { $sql = "INSERT INTO dtr (userID,timeIN) VALUES ('$userID', now())"; mysqli_query($con, $sql); mysqli_close($con); echo 'Time IN Successful!'; } } else { echo 'Invalid USER ID. Please try again!'; mysqli_close($con); }
I did it from my mobile not tested but you will get the idea of what is going on
if(isset($check)) { $sql="SELECT * FROM dtr WHERE userID = $userID"; $result = mysqli_query($con,$sql); $check = mysqli_fetch_array($result); if(isset($check)) { echo "Already in"; if(isset($check['timeIN']) && !isset($check['timeOUT'])) { $sql = "UPDATE dtr SET timeOUT= now() WHERE userID=$userID"; mysqli_query($con, $sql); mysqli_close($con); } } else { $sql = "INSERT INTO dtr (userID,timeIN) VALUES ('$userID', now())"; mysqli_query($con, $sql); mysqli_close($con); echo 'Time IN Successful!'; } } else { echo 'Invalid USER ID. Please try again!'; mysqli_close($con); }
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
我相信你正在寻找以下语法 INSERT INTO
(field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ... I believe you are looking for the following syntax INSERT INTO