-1

I'm somewhat new to C++ I decided to work on a small project and I'm currently trying to set up something to make an array, put 5 objects into it and then increase its size and put more objects into it; however, I've run into an issue where I cannot figure out how to put data into the array

This is what I've got so far:

#include <iostream>
#include "Token.h"

int main()
{
    Token* ts = new Token[5]; //create the initial array
    ts[0] = new Token(TT_PLUS, "+"); //add the item

    int size = sizeof(ts) / sizeof(Token); //get the new size
    size_t newSize = size * 2; // double it
    Token* newArr = new Token[newSize]; //create new array

    memcpy(newArr, ts, size * sizeof(Token)); //copy data

    size = newSize; //The array resizing is code I found, so I'm not sure why this is here...
    delete[] ts; //delete old array data
    ts = newArr; // array is now updated?
    std::cout << ts[0].type << std::endl; trying to get the type of  Token 0
}

This is my error(s):

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0349   no operator "=" matches these operands  ATestProgrammingLang    C:\Users\usr\source\repos\ATestProgrammingLang\ATestProgrammingLang.cpp 10
Severity    Code    Description Project File    Line    Suppression State
Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'Token *' (or there is no acceptable conversion) ATestProgrammingLang    C:\Users\usr\source\repos\ATestProgrammingLang\ATestProgrammingLang.cpp 10  

Let me know if I need to give you more information.

  • 1
    Have you tried to use STL containers? For example, std::vector, std::list, std::deque? – Amir Kadyrov Oct 06 '22 at 09:02
  • @akk0rd87 No, I actually haven't heard of them before, I'll do a quick google search for them, maybe they will help, thanks! – crispeeweevile Oct 06 '22 at 09:03
  • 1
    And you should step back and learn C++ from a good source, because there are many bad things in your code. (You must not add items into an array, an array is alread filled after creation; the sizeof calculation only work for static arrays not for dynamic ones; in gerneral you are not allowed to memcpy objects) – gerum Oct 06 '22 at 09:07
  • 2
    @crispeeweevile If you started C++ without ever hearing about standard containers like `std::vector` or `std::array` you might need a [good C++ book](https://stackoverflow.com/questions/388242). Sorry to say, but almost everything here is wrong, as gerum also mentioned. – Lukas-T Oct 06 '22 at 09:08
  • Typically we use std::vector as array that you describe. In modern C++ code explicit new and delete are used very rarely ... as containers and smart pointers have abstracted the need away. – Öö Tiib Oct 06 '22 at 09:09
  • `sizeof(ts) / sizeof(Token)` is equivalent to `sizeof(Token*) / sizeof(Token)`. You can't determine the size of an array from a pointer to one of its elements. You need a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Oct 06 '22 at 09:33
  • @molbdnilo Why is `sizeof(ts)` equivalent to `sizeof(Token*)`? Wouldn't it be equal to the size of the array (in this case 5) times the size of Token? – crispeeweevile Oct 06 '22 at 09:36
  • `ts` is not an array, it is a pointer. If an expression `e` has the type `T`, `sizeof(e)` is the same as `sizeof(T)`; the `sizeof` is determined entirely by the type. Thus, you have `sizeof(ts) == sizeof(Token*)`. – molbdnilo Oct 06 '22 at 09:37
  • @molbdnilo I see, so technically I should have done `sizeof(&ts)` assuming that's the right operator, I always forget... – crispeeweevile Oct 06 '22 at 09:39
  • @crispeeweevile No, thats' a `Token**`, which will have the same size as a `Token*`. As I said, you *can't* determine an array size from a pointer. – molbdnilo Oct 06 '22 at 09:41
  • Forget that C-style arrays exist in the language. Don't use them. Use `std::array` and/or `std::vector` instead. – Jesper Juhl Oct 06 '22 at 09:48
  • @JesperJuhl I seem to have come to the same conclusion that they're not good/not useful, why is that? – crispeeweevile Oct 06 '22 at 09:53
  • @crispeeweevile they are useful, but modern C++ just has much better, easier and safer alternatives, so IMHO they are more or less obsolete. – Jesper Juhl Oct 06 '22 at 10:58

1 Answers1

0

First of all I think you use C-style. Use STL containers (std::vector for example) instead of c arrays. I don't know what is Token class, but in my opinion you don't need to use new in this part:

ts[0] = new Token(TT_PLUS, "+"); //add the item
lmidori
  • 42
  • 7