返回向量<object>
Returning a vector<object>
我在网上搜索了如何返回矢量对象,但我找不到最简单的一个。首先,我不是C++专家,几周前我才开始C++。返回对象向量不行吗?
我有这样的东西。
我通过这个把它推到某个地方:
主文件:
int main()
{
XIniFile *f = new XIniFile();
int result = 0;
int v = 0;
char *val;
result = f->open("doc2.ini");
if (INI_FILE_RES_OK == result) {
}
else
printf("Error[%d]n", result);
}
CPP 文件:
XKey::XKey()
{
}
XKey::~XKey()
{
}
XSection::XSection()
{
}
XSection::~XSection()
{
}
XKey *XSection::addKey(const char *k, const char *val)
{
XKey *nk = new XKey;
nk->setName(k);
nk->setValue(val);
m_vkey.push_back(*nk);
return nk;
}
void XSection::showKey()
{
vector<XKey>::iterator ik;
for (ik = m_vkey.begin(); ik != m_vkey.end(); ik++)
printf("%s = %sn", ik->getName(), ik->getValue());
}
XIniFile::XIniFile()
{
m_modified = false;
}
int XIniFile::open(const char *fn)
{
XSection *cs;
char *sn;
char *kn;
char *val;
int i = 0;
FILE *f;
f = fopen(fn, "r");
if (!f)
return INI_FILE_ERROR;
struct stat file_stat;
stat(fn, &file_stat);
int size = file_stat.st_size;
m_name = strdup(fn);
char *d = (char *)malloc(size * sizeof(char *) + 1);
fread(d, size, 1, f);
while (i < size) {
while (d[i] != '[') i++;
if (d[i] == '[') {
i++;
while (isspace(d[i])) i++;
sn = &d[i];
while (d[i] != ']')
i++;
d[i++] = 0;
cs = addSection(sn);
while (isspace(d[++i]));
while(d[i] != '[') {
while (isspace(d[i])) i++;
kn = &d[i];
while (d[i] != '=') i++;
d[i-1] = 0;
i++;
while (isspace(d[i])) i++;
if (d[i] == '[') {
i++;
val = &d[i];
while (isspace(d[i])) i++;
d[i-1] = 0;
}
else {
val = &d[i];
while (d[i] != 'n') i++;
d[i] = 0;
}
i++;
cs->addKey(kn, val);
while (isspace(d[i])) {
i++;
if (i >= size) break;
}
if (i >= size) break;
}
}
}
free(d);
vector<XSection>::iterator is;
for (is = m_vsection.begin(); is != m_vsection.end(); is++) {
printf("[%s]n", is->getName());
printf("is->getSize()[%d]n", is->getSize());
}
fclose(f);
return INI_FILE_RES_OK;
}
XIniFile::~XIniFile()
{
delete m_name;
}
XSection *XIniFile::addSection(const char *s)
{
XSection *ns = new XSection;
ns->setName(s);
m_vsection.push_back(*ns);
return ns;
}
void XIniFile::showSection()
{
vector<XSection>::iterator is;
for (is = m_vsection.begin(); is != m_vsection.end(); is++)
printf("[%s]n", is->getName());
printf("Endn");
}
头文件:
class XKey
{
public:
XKey();
virtual ~XKey();
void *setName(const char *k) {m_name = strdup(k);}
void *setValue(const char *v) {m_value = strdup(v);}
char *getName(){return m_name;}
char *getValue(){return m_value;}
private:
char *m_name;
char *m_value;
};
class XSection
{
public:
XSection();
virtual ~XSection();
void *setName(const char *n) {m_name = strdup(n);}
char *getName() {return m_name;}
XKey *addKey(const char *k, const char *v);
vector<XKey> getKey() {return m_vkey;}
int getSize() {return m_vkey.size();}
void showKey();
private:
char *m_name;
vector<XKey> m_vkey;
};
class XIniFile
{
public:
XIniFile();
virtual ~XIniFile();
int open(const char *);
int readString(const char *, const char *, char **);
int readInt(const char *, const char *, int *);
XSection *addSection(const char *);
void showSection();
private:
char *m_name;
vector<XSection> m_vsection;
bool m_modified;
};
这里的问题是,在 CPP 文件下,即使我在addKey
方法上m_vkey
上使用push_back
,is->getSize()
也不会增加。
这是因为您正在修改的对象与您存储的对象不同。
看这里:
XSection *XIniFile::addSection(const char *s)
{
XSection *ns = new XSection; // Create a new XSection
ns->setName(s);
m_vsection.push_back(*ns); // Store a copy of ns
return ns; // Return the original ns
}
现在,当您对返回值执行某些操作时,m_vsection
中的对象不受影响。
最快的解决方法是将要返回的指针存储在m_vsection中,并且与部分对象类似。
尽管您可能应该开始使用std::shared_ptr
(更不用说std::string
了(,如果可能的话。
相关文章:
- EASTL矢量<向量<int>>连续的
- 当覆盖存在时调用基本虚拟"binded to object"函数
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- C++将派生类转换为基类时'object slicing'期间发生的情况
- 如何理解 C++17 标准参考中的 [intro.object]/3? N4659 的
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- C++线程"Call to non-static member function without an object argument"
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- Nan::ObjectWrap to Napi::Object Wrap 无法访问 JavaScript 中的属性
- 现代C++会支持 C# "Object Initalizer"语法吗?
- 指向对象生存期之外的已分配内存的指针是"invalid pointer[s]"还是"pointer[s] to an object"?
- C++ Equivalent of Java Map<String, Object>
- WebKit2GTK+ "window-object-cleared"未清除
- 返回 *&object 时是否允许复制/移动省略?
- QPointer::clear() 是删除其引用的指针,还是"Clears this QPointer object."其他含义?
- 解决方法:'can not be used when making a shared object; recompile with -fPIC'使用Cmake。使用普通的 g++ 工作
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型