gpt4 book ai didi

c++ - 错误 : cannot convert ‘std::basic_string’ to ‘int’ in assignment

转载 作者:行者123 更新时间:2023-11-28 07:37:02 25 4
gpt4 key购买 nike

我收到以下代码的上述错误。请帮我解决我做错了什么。此代码具有嵌套结构。目前,它会根据用户提供的输入为生成结构生成名称。

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <limits>
#include <stdio.h>
#include <sstream>


using namespace std;
using std::stringstream;
struct rootset {
double totSize;
const char *rStrtPtr;
const char *rEndPtr;

struct generations {
double totSize;
const char *genStrtPtr;
const char *genEndPtr;
int numOfGen;
int genName;

struct object {
double objSize;
const char *objStrtPtr;
const char *objEndPtr;
string id;
char markBit;
char objPtr;


struct freeList {
double freeSpace;
int flNumb;
};
};
} generation;
};

int main()
{

int gen =0;
cin >> gen;

rootset* pRootSet = (rootset*)malloc(1200);

for( i=0; i<gen; i++) {
stringstream out;
out << i;
string s = out.str();
string foo = "generation";

pRootSet->generation.genName = foo.append(s); /*Error is here*/
cout<<"foo: "<<foo<<endl;
}
}

我正在尝试打印这个答案:

4
foo: generation0
foo: generation1
foo: generation2
foo: generation3

最佳答案

generation.genName 是 INT 类型,而你想传递一个字符串值而不进行任何转换,这就是为什么它会报错。解决方法:先做转换。

关于c++ - 错误 : cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16600970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com