好久没氵博客了,最近要把业务的numpy函数写成cpp版本,终于碰到一个问题:
建议直接看这篇博客,很详细:
https://blog.csdn.net/kanglongyao/article/details/47184593
VScode 报错:
default argument given for parameter 1 of 'Tensor<float> make_coordmat(std::deque<int>)' [-fpermissive]
终端编译报错:
x.cpp:9:65: error: default argument given for parameter 1 of 'Tensor<float> make_coordmat(std::deque<int>)' [-fpermissive]
Tensor<float> make_coordmat(std::deque<int> shape = {1, 80, 200}){
^
In file included from x.cpp:5:
./x.h:10:15: note: previous specification in 'Tensor<float> make_coordmat(std::deque<int>)' here
Tensor<float> make_coordmat(std::deque<int> shape={1, 80, 200});
我的错误代码:
在 x.cpp
中:
Tensor<float> make_coordmat(std::deque<int> shape = {1, 80, 200}){
Tensor<float> x_coord;
......
return x_coord;
}
在x.h
中:
Tensor<float> make_coordmat(std::deque<int> shape={1, 80, 200});
查了一下,由于申明和函数体,只能有一个可以写默认值,二者不可都写,所以将其中一个去掉即可,即:
在 x.cpp
中:
Tensor<float> make_coordmat(std::deque<int> shape){
Tensor<float> x_coord;
......
return x_coord;
}
或者在x.h
中:
Tensor<float> make_coordmat(std::deque<int> shape);
去掉那个 ={1, 80, 200}
即可
然而当我在main函数中调用时:
Tensor<float> xxxxx = make_coordmat();
VScode报错:
too few arguments to function 'Tensor<float> make_coordmat(std::deque<int>)'
意思是参数没填够
终端报错:
main.cpp: In function 'int main()':
main.cpp:119:41: error: too few arguments to function 'Tensor<float> make_coordmat(std::deque<int>)'
Tensor<float> xxxxx = make_coordmat();
^
In file included from main.cpp:15:
./x.h:9:15: note: declared here
Tensor<float> make_coordmat(std::deque<int> shape);
所以我只能在申明中写上默认值 ={1, 80, 200}
, 而不是函数体中写默认值
这样改之后,可以运行,get√
有参考自:
https://stackoverflow.com/questions/2545720/error-default-argument-given-for-parameter-1
https://blog.csdn.net/kanglongyao/article/details/47184593