-1

Basically I am trying to make an array which will get larger every time the user enters a value. Which means I never know how long the array will be. I don't know if this is something to do with my class or anything.

#pragma once
#include <iostream>
#include <string>
#define dexport __declspec(dllexport)
//Im making an DLL
using namespace std;
class dexport API {
private:
    string users[] = {"CatMan","ManCat"}; //Line With Error Incomplete Type Is Not Allowed
public:
    string getAllUsers(string list[]) {
        for (unsigned int a = 0; a < sizeof(list) / sizeof(list[0]); a = a + 1) {
            return list[a];
        }
    }
};

It gives me an error Incomplete type is not allowed. I really have no idea what to do. Compiler Error

bolov
  • 72,283
  • 15
  • 145
  • 224
sbndh
  • 19
  • 1
  • 4
  • 5
    What you want is a `std::vector` – NathanOliver Oct 22 '19 at 19:28
  • By the way, `getAllUsers` only returns the first element. – iz_ Oct 22 '19 at 19:52
  • You have a consensus. But if you are not allowed to use the containers library, there is always `new` and `delete` (they have largely been supplanted by the containers like `std::vector`, but there is 20+ years of code out there that uses them) – David C. Rankin Oct 22 '19 at 20:14
  • Possible duplicate of [Create an array when the size is a variable not a constant](https://stackoverflow.com/questions/57367473/create-an-array-when-the-size-is-a-variable-not-a-constant) –  Oct 22 '19 at 20:52

3 Answers3

2

There are a few things wrong with your code. For starters, an array has a fixed size, so, even if your code did compile, it wouldn't work. Normally, the compiler would infer the size of your array from the length of the initializer; but you are creating a class, and it needs to know it, hence the error.

This will solve your compilation problem:

string users[2] = {"CatMan","ManCat"};

But then your array has a fixed size, and that is not what you want, so you need an std::vector:

#include <vector>

[...]

vector<string>users = {"CatMan","ManCat"};

Now you can use the '[]' operator to access the strings, and users.push_back to add new users.

The next problem you need to solve is the way you are trying to return your value: you shouldn't use an argument as an out value (although you can, with either a reference or a pointer). You should decide whether you want to return a reference to your vector, a copy of your vector, or a const reference, for example:

// Returning a copy
vector<string> getAllUsers() {
    return users;
}

// Returning a reference
vector<string>& getAllUsers() {
    return users;
}

Finally, you are creating a library: you should know that if you want to share memory between different processes, you need to use some kind of shared memory. Currently, every program will keep its own copy of the API.

Josu Goñi
  • 1,178
  • 1
  • 10
  • 26
0

What you are looking for is an std::vector.

You can find more info here.

It's somewhat similar to an array, except that it allows variable length.

Simon
  • 405
  • 2
  • 8
0

You can use std::vector. It allocate and copy elements to new place if it got out space.

If you wanna make the class yourself for educational reason here is what you should try as a basic solution:

You allocate some memory up front and store its length as capacity. You need a variable(size) to store the number of elements already entered to the class e.g. via a push_back function. Once the size reached capacity, you need to reallocate memory copy over all the elements and then delete the old memory.

Oblivion
  • 7,176
  • 2
  • 14
  • 33