c – 模板上的宏
发布时间:2020-12-16 09:37:15 所属栏目:百科 来源:网络整理
导读:不确定我想做什么是不好的但这是我的问题: 我有一些模板功能,如 std::vectorT operator - (const std::vectorT data1,const std::vectorT data2);std::vectorT operator * (const std::vectorT data1,const std::vectorT data2);std::vectorT operator (con
不确定我想做什么是不好的但这是我的问题:
我有一些模板功能,如 std::vector<T> operator - (const std::vector<T>& data1,const std::vector<T>& data2); std::vector<T> operator * (const std::vector<T>& data1,const std::vector<T>& data2); std::vector<T> operator & (const std::vector<T>& data1,const std::vector<T>& data2); ….等等.除了运算符之外,所有这些函数都具有完全相同的定义,因此我尝试编写这样的宏 #define _BINARY_OP_ON_DATASET (OP_TYPE) template <typename T> std::vector<T> operator OP_TYPE (const std::vector<T>& data1,const std::vector<T>& data2) { std::vector<T> result; result.push_back(data1.begin().val OP_TYPE data1.begin().val)/*sample implementation*/ return result; } _BINARY_OP_ON_DATASET (&) _BINARY_OP_ON_DATASET (+) 而且我得到了一堆错误 Error 1 error C2833: 'operator OP_TYPE' is not a recognized operator or type Error 2 error C2988: unrecognizable template declaration/definition Error 3 error C2059: syntax error : 'newline' Error 5 error C2143: syntax error : missing ';' before '<' Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int …和更多 谢谢你的帮助. 简历 解决方法
宏参数列表前没有空格!
#define _BINARY_OP_ON_DATASET(OP_TYPE) ... ^^^ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |