c++中的指针对
Pointer in a pair in C++
我需要返回一个数组和它的大小:
pair<int*, int> load(string path,int * memory){
ifstream handle;
int container;
int size = 0;
if(!handle){
QMessageBox::warning(0, QString(""), "file cannot be loaded");
return make_pair(NULL,-1);
}
handle.open(path.c_str());
while(!handle.eof()){
handle >> container;
size++;
}
size--;
if(!size){
QMessageBox::warning(0, QString(""), "file is empty");
return make_pair(NULL,-1);
}
memory = new int[size];
handle.clear();
handle.seekg(0, ios::beg);
for(int i = 0; i < size; i++){
handle >> memory[i];
}
handle.close();
return make_pair(memory, size);
}
错误输出为:
/usr/include/c++/4.6/bits/stl_pair.h:109: error: invalid conversion从'int'到'int*' [-fpermissive]
我该怎么做?
由于NULL可能定义为:
#define NULL 0
表达式make_pair(NULL,-1)
变成make_pair(0, -1)
,因此它产生pair<int, int>
。例如,如果可用,可以使用nullptr
,否则可以使用(int*)NULL
。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错