
vector容器两处报错,一、表达式必须包含指向对象的指针类型。二、指向绑定函数的指针只能用于调用函数
#include<iostream>#include<vector>#include<string>usingnamespacestd;classPOINT1{publi...
#include<iostream>
#include<vector>
#include<string>
using namespace std ;
class POINT1
{
public:
int n ;
vector<string> s (int a) ;
POINT1 () {n = 4 ; s (n) ; s[3] = "hello" ;}
} ;
int main ()
{
POINT1 Point1 ;
Point1.n = 3 ;
Point1.s[2] = "hello" ;
}第一处报错是s[3] = "hello"的s处;第二处报错是Point1.s[2] = "hello"的"."处,给位帮帮忙,然后告诉我错误原因,非常感谢!! 展开
#include<vector>
#include<string>
using namespace std ;
class POINT1
{
public:
int n ;
vector<string> s (int a) ;
POINT1 () {n = 4 ; s (n) ; s[3] = "hello" ;}
} ;
int main ()
{
POINT1 Point1 ;
Point1.n = 3 ;
Point1.s[2] = "hello" ;
}第一处报错是s[3] = "hello"的s处;第二处报错是Point1.s[2] = "hello"的"."处,给位帮帮忙,然后告诉我错误原因,非常感谢!! 展开
2014-11-21
展开全部
s应该是一个返回类型是vector<string>,参数是int的函数吧。
那s[3] = "hello"实在不理解这是什么意思,你是把s当成vector<string>用了吗
那s[3] = "hello"实在不理解这是什么意思,你是把s当成vector<string>用了吗
更多追问追答
追问
如果我想要一个string“数组”我可以写成vector s(5)是吧,本身也可以写成vector s(n)是吧,只要之前我已经先给出n的值。但是如果我是希望在一个类中定义一个这样的未知个数的数组,我没法先给出n,所以我想这样写vector s (int a) ,然后就是用s (a)再给出s的定义,这样如果不对,要想实现这样的功能该怎样操作呢?
追答
你可以试一下这样写
class POINT1
{
public:
int n;
vector s ;
POINT1 () {s.resize(n) ; s[3] = "hello" ;}
} ;
int main ()
{
POINT1 Point1;
Point1.n = 4 ;
Point1.s[2] = "hello" ;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询