在c++的main()函数中传递指针
passing pointers through multiple functions from main() C++
这不是很好。让我们看看我们是否可以共同扩展我们对这个问题的认识。好:
vector<vector<Point>> aVectorOfPoints
int main(){
someConstructor(&aVectorOfPoints)
}
someConstructor(vector<vector<Point>>* aVectorOfPoints){
functionOne(aVectorOfPOints);
}
functionOne(vector<vector<Point>>* aVectorOfPOints){
aVectorOfPoints[i][j] = getPointFromClass();
}
//functionX(...){...}
我在functionOne的赋值下面得到一些错误。我怎样才能做得更好?谢谢。
具体错误是"No operator '='匹配这些操作数"
为什么这是错误的?
aVectorOfPoints[i][j] = getPointFromClass();
类型的aVectorOfPoints
为vector<vector<Point>>*
。aVectorOfPoints[i]
类型为vector<vector<Point>>
。aVectorOfPoints[i][j]
的类型为vector<Point>
。
Point
不能分配给vector<Point>
。因此会出现编译错误。
也许你想用:
(*aVectorOfPoints)[i][j] = getPointFromClass();
你可以通过传递引用来简化代码。
int main(){
someConstructor(aVectorOfPoints)
}
someConstructor(vector<vector<Point>>& aVectorOfPoints){
functionOne(aVectorOfPOints);
}
functionOne(vector<vector<Point>>& aVectorOfPOints){
aVectorOfPoints[i][j] = getPointFromClass();
}
使用引用代替指针:
someConstructor( vector<vector<Point>> &aVectorOfPoints) {
和functionOne
相同。
你的错误是aVectorOfPoints[i]
通过i
索引指针。如果使用指针,在此之前需要先对指针解引用,写入(*aVectorOfPoints)[i][j]
.
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址