- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
问题在评论中回答 由于我的声誉,我无法以常规方式回答。我稍后会在答案中添加详细信息,已在评论中解决。谢谢。**
大家好-
根据这个问题,您无疑会看到,我是 C++ 的新手,但有使用一些高级语言的经验。 (这似乎是伤害多于帮助)
对于一个类,我需要为类型为整数的数组创建一个包装器。 (在类的这个阶段没有模板)我还需要让类有一个非零的起始索引。我在类中使用一个成员数组来存储我的数据(此时类中还没有 vector )并从公共(public)方法中进行一些转换以访问适当的内部数组元素。
我遇到的问题是我在编译时不知道内部数组大小,所以我将它声明为类全局指针并在构造函数中设置大小。下面是问题区域中的代码片段:
int *list;
safeArray::safeArray(int start, int initialSize)
{
if(initialSize <= 0)
{
throw "Array size must be a positive integer";
}
maxSize = initialSize + 1;
startIndex = start;
endIndex = start + initialSize;
list = new int[maxSize]; // Error thrown here
int *tempArray = new int[maxSize];
copyArray(tempArray);
clearArray();
}
我得到的错误是
将“int*”赋值给“int[0u]”时类型不兼容
我不是 100% 确定 int[0u] 的类型是什么。是字面值零和 u 是无符号的吗?我已经在调试器中检查过 maxSize 有一个值,我也用一个常量整数值替换了它并得到了同样的错误。
因为我的 int *tempArray = new int[maxSize];
行有效,我认为这可能与需要同时声明和大小有关,所以我选择做一个内存文件。 (这实际上超出了赋值范围,所以一定还有其他我遗漏的东西)memcpy 失败,因为看起来我正在破坏我的其他变量。当我在 GDB 中打印 list 的地址时,它给我的地址与我代码中的另一个全局变量相同,因此该路由似乎也超出了赋值范围。
我在其他论坛上看到的共同主题是您不能像其他变量一样分配数组,但我认为这不会包括 new
语句。我的假设错了吗?
我目前看到的唯一编译错误是上面提到的那个,我在代码中的每个 list = new int[maxSize];
语句中都看到了它。
我的问题是:
什么是 int[0u] 类型,该类型是在哪里生成的?它必须来自新声明,对吗?
在类中使用动态数组资源的最佳方式是什么?除了使用 vector ? =)
我想这就是所有相关信息,但如果我错过了关键数据,我深表歉意。下面是其余的实现代码。
/*
* safeArray.cpp
* safearray
*
* Created by Jeffery Smith on 6/1/11.
*
*
*/
#include "safeArray.h"
#include <iostream>
using namespace std;
int startIndex = 0;
int endIndex = 0;
int maxSize = 1;
int currentSize = 0;
int *list;
safeArray::safeArray(int start, int initialSize)
{
if(initialSize <= 0)
{
throw "Array size must be a positive integer";
}
maxSize = initialSize + 1;
startIndex = start;
endIndex = start + initialSize;
list = new int[maxSize]; // Error thrown here
int *tempArray = new int[initialSize + 1];
copyArray(tempArray);
clearArray();
}
safeArray::safeArray(const safeArray &sArray)
{
list = new int[sArray.maxSize];
copyArray(sArray);
startIndex = sArray.startIndex;
endIndex = sArray.endIndex;
maxSize = sArray.maxSize;
currentSize = sArray.currentSize;
}
void safeArray::operator=(const safeArray &right)
{
list = new int[right.maxSize];
copyArray(right);
startIndex = right.startIndex;
endIndex = right.endIndex;
maxSize = right.maxSize;
currentSize = right.currentSize;
}
safeArray::~safeArray()
{
delete [] list;
}
int safeArray::operator[](int index)
{
if(OutofBounds(index))
{
throw "You tried to access an element that is out of bounds";
}
return list[index - startIndex];
}
void safeArray::add(int value)
{
if(this->isFull())
{
throw "Could not add element. The Array is full";
}
currentSize++;
list[currentSize + startIndex];
}
void safeArray::removeAt(int value)
{
if(OutofBounds(value))
{
throw "The requested element is not valid in this list";
}
compressList(value);
currentSize--;
}
void safeArray::insertAt(int location, int value)
{
if(OutofBounds(location) || this->isFull())
{
throw "The requested value is either out of bounds or the list is full";
}
expandList(location, value);
currentSize++;
}
void safeArray::clearList()
{
clearArray();
}
bool safeArray::isFull()
{
return(maxSize == currentSize);
}
int safeArray::length()
{
return currentSize;
}
int safeArray::maxLength()
{
return this->maxSize;
}
bool safeArray::isEmpty()
{
return(currentSize == 0);
}
bool safeArray::OutofBounds(int value)
{
return (value > endIndex || value < startIndex);
}
void safeArray::clearArray()
{
for(int i = 0; i < maxSize; i++)
{
list[i] = 0;
}
currentSize = 0;
}
void safeArray::compressList(int value)
{
for(int i = value; i < endIndex; i++)
{
list[i] = list[i + 1];
}
}
void safeArray::expandList(int location, int value)
{
int tempHolder = list[location];
list[location] = value;
for(int i = location; i < endIndex; i++)
{
tempHolder = list[location];
list[location] = value;
value = tempHolder;
}
}
void safeArray::copyArray(int *srcAddr )
{
memcpy(list, srcAddr, sizeof(int) * maxSize);
}
void safeArray::copyArray(const safeArray &sArray)
{
memcpy(list, &sArray, sizeof(int) * maxSize);
}
这是标题定义:
/*
* safeArray.h
* safearray
*
* Created by Jeffery Smith on 6/1/11.
* Copyright 2011 Accenture. All rights reserved.
*
*/
class safeArray {
public:
safeArray(int,int); //Standard constructor
~safeArray(); //Destructor
int operator[](int);
void operator=(const safeArray&); //Assignment overload
safeArray(const safeArray &sArray); //Copy Constructor
void add(int);
int maxLength();
int length();
bool isFull();
bool isEmpty();
void clearList();
void removeAt(int);
void insertAt(int,int);
protected:
int list[];
int startIndex;
int endIndex;
int maxSize;
int currentSize;
private:
void clearArray();
bool OutofBounds(int);
void expandList(int,int);
void compressList(int);
void copyArray(int*);
void copyArray(const safeArray&);
};
最佳答案
int[0u]
?我相信您可以在 C 中在结构的末尾使用零长度数组,以有效地允许使用可变大小的结构,但这在 C++ 中没有做到。我在您的代码中没有看到任何非法代码。可怕的,是的,非法的,不。您需要发布safearray.h
的内容,如果它包含标准 header ,则您使用 using namespace std;
很容易成为问题的原因。
此外,全局变量也不好。只需将指针放在类中 - 除非您做错了一些事情,否则您基本上不必使用全局变量。尤其是因为它会让您面临可变阴影、名称冲突和其他大量问题。哦,你应该抛出一个异常类,最好派生自 std::exception
或 std::runtime_error
.没有人会试图捕获 const char*
.你不应该使用 std
命名空间-你在乞求问题。而且您不是在调用复制构造函数或赋值运算符,而是使用 memcpy 复制您的元素?您还在几个地方泄漏了内存 - 从赋值运算符开始。
template<typename T> class safe_array {
char* list;
std::size_t arrsize;
void valid_or_throw(std::size_t index) {
if (index <= arrsize) {
throw std::runtime_error("Attempted to access outside the bounds of the array.");
}
public:
safe_array(std::size_t newsize)
: list(NULL) {
size = arrsize;
list = new char[arrsize];
for(std::size_t i = 0; i < arrsize; i++) {
new (&list[i * sizeof(T)]) T();
}
}
safe_array(const safe_array& ref)
: list(NULL) {
*this = ref;
}
safe_array& operator=(const safe_array& ref) {
clear();
arrsize = ref.size;
list = new char[arrsize];
for(std::size_t i = 0; i < arrsize; i++) {
new (&list[i * sizeof(T)]) T(ref[i]);
}
}
T& operator[](std::size_t index) {
valid_or_throw(index);
return static_cast<T&>(list[index * sizeof(T)]);
}
const T& operator[](std::size_t index) {
valid_or_throw(index);
return static_cast<const T&>(list[index * sizeof(T)]);
}
void clear() {
if (list == NULL)
return;
for(std::size_t i = 0; i < size; i++) {
(*this)[i].~T();
}
delete[] list;
list = NULL;
arrsize = 0;
}
std::size_t size() {
return arrsize;
}
bool empty() {
return (list == NULL);
}
~safe_array() {
clear();
}
};
我创建的一个相对较快的示例 类应该能为您指明大致的方向。它不提供 vector
的所有功能,例如没有自动调整大小或容量缓冲(以及其他一些缺点),我非常有信心我可能忘记了几件事,但这只是一个开始。
关于c++ - 在类中使用动态数组——在编译时接收错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6236844/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!