Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++在数组中添加新的类对象到新的点 我是C++编程新手,对AS3编程有一定的了解。 我的问题是,我不知道如何将新对象从类插入数组_C++_Arrays_Class_Visual C++ - Fatal编程技术网

C++在数组中添加新的类对象到新的点 我是C++编程新手,对AS3编程有一定的了解。 我的问题是,我不知道如何将新对象从类插入数组

C++在数组中添加新的类对象到新的点 我是C++编程新手,对AS3编程有一定的了解。 我的问题是,我不知道如何将新对象从类插入数组,c++,arrays,class,visual-c++,C++,Arrays,Class,Visual C++,基本上我想做的是: ClassName classArray[]; classArray[n]=new ClassName("Tekst"); 以下是我使用visual Studio 2012 C++编写的代码: #include <iostream> #include <string> using namespace std; //a class holding user data class User { public: string name;

基本上我想做的是:

ClassName classArray[];
classArray[n]=new ClassName("Tekst");
以下是我使用visual Studio 2012 C++编写的代码:

#include <iostream>
#include <string>
using namespace std;

//a class holding user data
class User
{
public:
    string name;
    User(string nameInn)
    {
        //when the user is created it should get information about its name.
        name=nameInn;
    }
};

//array with all users
User userArr[];
int userArrLength=0; //the length of that array (dont know how to find the length of arays holding classes)

int main()
{
    //the user writes down the name of all users.
    cout << "Write user name. \n Write \"fin\" til finish\n";
    bool hasFinished=false;
    //asks you for a new user until you write fin
    while(hasFinished==false)
    {
        string inn;
        cin >> inn;
        if(inn=="fin") hasFinished=true;
        //here im trying to make a new user inn a new spot in the userArr.
        else userArr[(userArrLength+=1)+1]=new User(inn);
    }

    return 0;
}
我的格式是否有误?如果有,我如何设置格式?还是我误解了C++中的一些重要的东西?

是一个数据结构,它实现了一个动态大小的数组,可以根据需要增长大小。您可以使用std::vector而不是自己的数组:

#include <vector>

...

  std::vector<User> v;

  // perhaps in a loop
  string inn;
  cin >> inn;
  v.push_back(User(inn));
是一种实现动态大小的阵列的数据结构,该阵列可以根据需要增大大小。您可以使用std::vector而不是自己的数组:

#include <vector>

...

  std::vector<User> v;

  // perhaps in a loop
  string inn;
  cin >> inn;
  v.push_back(User(inn));

<>你不能改变数组的长度,一旦创建了。< /p> 在创建数组之后,你不能改变数组的长度。C++中的

< p>数组是静态大小的。此外,new会创建一个指针,这意味着您为数组另外使用了错误的数据类型

建议的解决方法是使用向量,而不是:

// At the top
#include <vector>

// Instead of that array
std::vector<User> userVector;

// Inside of the loop
userVector.push_back(User(inn));
vector本质上是一个动态数组

然而,仍然有一些需要考虑的问题:当为向量分配空间时,它的所有成员都使用默认构造函数初始化,即可以不带参数调用的构造函数


如果您的用户类没有默认构造函数(如果是cl),则必须插入指向用户std::vector userVector和userVector.push_backnew Userinn的指针,然后使用delete手动删除指针。在C++中,请参阅下面的注释

,数组是静态大小的。此外,new会创建一个指针,这意味着您为数组另外使用了错误的数据类型

建议的解决方法是使用向量,而不是:

// At the top
#include <vector>

// Instead of that array
std::vector<User> userVector;

// Inside of the loop
userVector.push_back(User(inn));
vector本质上是一个动态数组

然而,仍然有一些需要考虑的问题:当为向量分配空间时,它的所有成员都使用默认构造函数初始化,即可以不带参数调用的构造函数


如果您的用户类没有默认构造函数(如果是cl),则必须插入指向用户std::vector userVector和userVector.push_backnew Userinn的指针,然后使用delete手动删除指针。请参阅下面的注释

如果不知道数组的大小,则不应使用静态数组。您应该使用动态数组。大概是这样的:

#include <vector>

std::vector<User> userArr;

如果不知道数组的大小,则不应使用静态数组。您应该使用动态数组。大概是这样的:

#include <vector>

std::vector<User> userArr;

你肯定会在这个程序中出错。你能把它们都列出来吗?我想你必须从头开始删除java,然后从C++的教学书籍开始。在这里,相似的事物的行为完全不同。当你看第一个例子时,首先想到的是:新的X返回一个指向X的指针,所以你需要一个指向X的指针数组,而不是一个X数组。@confusopoly也许他们根本不需要新的?@juanchopanza可能,但是,您会得到一个默认构造对象的数组,并为数组中的每个对象调用类的赋值运算符。这是我通常宁愿避免的,除非我知道类正确地实现了它。编辑:nvm,该类只包含一个std::string成员,因此不会引起任何问题。您肯定会在该程序中遇到错误。你能把它们都列出来吗?我想你必须从头开始删除java,然后从C++的教学书籍开始。在这里,相似的事物的行为完全不同。当你看第一个例子时,首先想到的是:新的X返回一个指向X的指针,所以你需要一个指向X的指针数组,而不是一个X数组。@confusopoly也许他们根本不需要新的?@juanchopanza可能,但是,您会得到一个默认构造对象的数组,并为数组中的每个对象调用类的赋值运算符。这是我通常宁愿避免的,除非我知道类正确地实现了它。编辑:nvm,该类只包含一个std::string成员,因此不会引起任何问题。最后两段是错误的。vector不要求类具有默认构造函数。分配原始内存,然后使用copyor move构造函数就地构造对象。唯一需要默认构造函数的时候是调用resize的单参数版本。感谢您的帮助!:最后两段是错的。vector不要求类具有默认构造函数。分配原始内存,然后使用copyor move构造函数就地构造对象。唯一需要默认构造函数的时候是调用resize的单参数版本。感谢您的帮助!:Dstd::字符串输入;标准::cin>>输入和输入!=鳍那是什么意思
伊恩?在这些情况下,您还需要使用std::吗?@tubeof?所有基本的\u istream对象都有一个布尔运算符,当调用该运算符时,该运算符将返回流是否有任何错误。&&运算符尝试将其转换为布尔值。这一行基本上是说,只要用户输入in的有效输入,只要in不等于输入后的fin,就这样做……std::string in;标准::cin>>输入和输入!=鳍这是什么意思?在这些情况下,您还需要使用std::吗?@tubeof?所有基本的\u istream对象都有一个布尔运算符,当调用该运算符时,该运算符将返回流是否有任何错误。&&运算符尝试将其转换为布尔值。这一行基本上是说,只要用户输入in的有效输入,并且输入后in不等于fin,就这样做……感谢您的帮助!:谢谢你的帮助!: