c++如何格式化指向类数组的指针?

时间:2022-09-01 11:27:27

I want to read information from a file and put that information in an pointer to an array of classes. it keeps throwing an exception at us->setUserName(name); but if I change it to us[i]->setUserName(name); it says "expression must have pointer type". How can I fix this?

我想从文件中读取信息,并将这些信息放在指向类数组的指针中。它不断向我们抛出异常->setUserName(name);但如果我把它改成[I]->setUserName(name);它说“表达式必须有指针类型”。我该怎么解决这个问题呢?

    const int SIZE = 100;
    User *us = new User[SIZE];

    input.open("Account.txt", ios::in);

    if (input)
    {
       input >> size;

       for (int i = 0; i < size; i++)
       {
          us = new User[i];
          getline(input, uName);
          us->setUserName(uName);

          getline(input, password);
          us->setPassword(password);

          getline(input, name);
          us->setName(name);
       }
    else 
       cout << "Error opening file" << endl;

here is the user class:

下面是用户类:

    class User
    {
    public:
      User();
      ~User();

      void setName(string);
      string getName();

      void setUserName(string);
      string getUserName();

      void setPassword(string);
      string getPassword();

      void setFollower(vector<User*>*);
      vector<User*>* getFollower();

      void setFollowing(vector<User*>*);
      vector<User*>* getFollowing();

    protected:
      string name;
      string userName;
      string password;

      vector <User*>* followers;
      vector <User*>* following;

  };

3 个解决方案

#1


4  

      us = new User[i];

Here you throw away all the arrays you have created by this moment and allocate a new array and thus eventually get N(O²) leaked space.

这里你扔掉你所有的数组由这一刻和分配一个新数组,从而最终得到N(O²)泄露的空间。

First, remove this line. Next, you might use us[i] in the succeeding property settings but since it will be a reference to an array element, not a pointer, you'll need to call its methods via ., not ->.

首先,删除这一行。接下来,您可以在后续的属性设置中使用us[i],但由于它将是对数组元素的引用,而不是指针,所以您需要通过.,而不是->调用它的方法。

BTW, in the listing you've provided one brace is missing, the one before else.

顺便说一句,在清单中,你提供了一个括号不见了,之前的一个。

As a side note, you are using too many plain pointers in your class. You did manage to handle and cleanup them properly, didn't you?

作为补充说明,您在类中使用了太多的纯指针。你确实把它们处理得很好,清理得很好,不是吗?

As another side note, it is generally a bad idea to store usernames+passwords as plain text (are passwords hashes here?). :)

另一方面,将用户名和密码存储为纯文本通常是一个坏主意(这里有密码吗?):)

#2


1  

us = new User[i];

Your problem here. This code will create an array has i element, every element is a new object User, and then pointer us will point to this. I suggest you use vector, it's dynamic array, it's better than a static array with SIZE = 100 in case you are not sure about input file (Account.txt could be contain more than 100 user information)

你的问题在这里。这段代码将创建一个具有i元素的数组,每个元素都是一个新的对象用户,然后指针us指向这个。我建议你使用矢量,它是动态数组,它比大小为100的静态数组更好,如果你不确定输入文件(Account)。txt可以包含超过100个用户信息)

std::vector<User> v;  // create a vector of User

input.open("Account.txt", ios::in);

if (input)
{
    input >> size;
    User user;  // a temporary user
    for (int i = 0; i < size; i++)
    {
        getline(input, uName);
        user->setUserName(uName);

        getline(input, password);
        user->setPassword(password);

        getline(input, name);
        user->setName(name);
        v.push_back(user); // push to vector
    }
}
else
    cout << "Error opening file" << endl;

#3


0  

if you want to continue to use an array do this

如果您想继续使用数组,请这样做

  if (input)
    {
       input >> size;

       for (int i = 0; i < size; i++)
       {
          getline(input, uName);
          us[i].setUserName(uName);
.....

#1


4  

      us = new User[i];

Here you throw away all the arrays you have created by this moment and allocate a new array and thus eventually get N(O²) leaked space.

这里你扔掉你所有的数组由这一刻和分配一个新数组,从而最终得到N(O²)泄露的空间。

First, remove this line. Next, you might use us[i] in the succeeding property settings but since it will be a reference to an array element, not a pointer, you'll need to call its methods via ., not ->.

首先,删除这一行。接下来,您可以在后续的属性设置中使用us[i],但由于它将是对数组元素的引用,而不是指针,所以您需要通过.,而不是->调用它的方法。

BTW, in the listing you've provided one brace is missing, the one before else.

顺便说一句,在清单中,你提供了一个括号不见了,之前的一个。

As a side note, you are using too many plain pointers in your class. You did manage to handle and cleanup them properly, didn't you?

作为补充说明,您在类中使用了太多的纯指针。你确实把它们处理得很好,清理得很好,不是吗?

As another side note, it is generally a bad idea to store usernames+passwords as plain text (are passwords hashes here?). :)

另一方面,将用户名和密码存储为纯文本通常是一个坏主意(这里有密码吗?):)

#2


1  

us = new User[i];

Your problem here. This code will create an array has i element, every element is a new object User, and then pointer us will point to this. I suggest you use vector, it's dynamic array, it's better than a static array with SIZE = 100 in case you are not sure about input file (Account.txt could be contain more than 100 user information)

你的问题在这里。这段代码将创建一个具有i元素的数组,每个元素都是一个新的对象用户,然后指针us指向这个。我建议你使用矢量,它是动态数组,它比大小为100的静态数组更好,如果你不确定输入文件(Account)。txt可以包含超过100个用户信息)

std::vector<User> v;  // create a vector of User

input.open("Account.txt", ios::in);

if (input)
{
    input >> size;
    User user;  // a temporary user
    for (int i = 0; i < size; i++)
    {
        getline(input, uName);
        user->setUserName(uName);

        getline(input, password);
        user->setPassword(password);

        getline(input, name);
        user->setName(name);
        v.push_back(user); // push to vector
    }
}
else
    cout << "Error opening file" << endl;

#3


0  

if you want to continue to use an array do this

如果您想继续使用数组,请这样做

  if (input)
    {
       input >> size;

       for (int i = 0; i < size; i++)
       {
          getline(input, uName);
          us[i].setUserName(uName);
.....