1

In C++, I'm having trouble with pointers etc. How can I fix the following problem?

error: no match for 'operator=' in '(stage->Stage::tiles + ((unsigned int)(((unsigned int)t) * 12u))) = (operator new(12u), (, ((Tile*))))'| note: candidates are: Tile& Tile::operator=(const Tile&)|*

stage.h

#include "Tile.h"

class Stage {
    public:
        Tile *tiles;
        int size;
        void init(int size);
};

stage.cpp

void Stage::init(int size) {
    this->size = size;
    this->tiles = new Tile[size];
}

application.cpp

#include "Stage.h"
#include "Tile.h"

bool setTiles( Stage * stage ) {

    for( int t = 0; t < stage->size; t++ ) {
        stage->tiles[t] = new Tile();
    }

    return true;
}

stage.init(1234);
setTiles( &stage );

Also, I don't really know when to use object.attribute and when to use object->attribute?

8
  • Stage doesn't have a member variable called size. Please post your actual code. Commented Aug 19, 2011 at 13:07
  • Sorry, I made this version particularly so that its easier to understand. Fixed it now. Commented Aug 19, 2011 at 13:10
  • 2
    Recovering Java programmer? If you just want tiles to be an array of tiles, you don't have to do anything beyond new Tile[size] -- that already invokes the default constructors for every element in the array. The entire setTiles function as you wrote it seems to be unnecessary. Commented Aug 19, 2011 at 13:12
  • 2
    Please use std::vector! You will save yourself a lot of hassle! Commented Aug 19, 2011 at 13:13
  • @Ferdinand Beyer- and if a dependency on Boost isn't an issue, take it a step further and use boost::ptr_vector. Commented Aug 19, 2011 at 14:45

4 Answers 4

6
stage->tiles[t] = new Tile();

You're calling new on something that's not a pointer. True, tiles is a pointer to an array, however, each element of that array is NOT a pointer. In order for that work, you would need an array of pointers, or a pointer to a pointer ,such as:

Tile **tiles;

What you could also do is create a separate pointer object, allocate it, and then copy the data to your array element by using

stage->tiles[i] = *somePointer;

and then deleting the pointer afterwards to free that allocated memory. This will preserve the copy because you invoked the copy constructor.

Sign up to request clarification or add additional context in comments.

Comments

3

You are trying to allocate a pointer with a pointer to an array. Try this one:

class Stage {
    public:
        Tile **tiles;
        void init(int size);
};

13 Comments

Where is he "trying to allocate a pointer with a pointer to an array"?
@Oli Charlesworth This is actually pretty accurate (at least his fix is, his explanation isn't), read my answer. +1
@Oli Charlesworth: new Tile[size] creates a pointer to an array of Tile with size elements Tile *tiles is a array or a pointer to ONE Tile
@MGZero: Ok, but this answer is not complete (and neither is yours, actually). The assignment in init() needs to become this->tiles = new Tile*[size]
@Ben: C++ is very complex and as a beginner, there are many pitfalls, especially when using dynamic memory. I would strongly recommend a good book; recommendations can be found at stackoverflow.com/questions/388242/… . For a basic tutorial, consider cplusplus.com/doc/tutorial
|
1
stage->tiles[t] = new Tile();

The above is not a valid C++ code, which you are perhaps confusing with the way new is used in other language such as C#. Though new can be used to allocate dynamic memories, but assigning an object to a particular element in the dynamically created array doesn't need the new construct. In fact, the object is already created as soon as you called new Tile[size]. What you may want to do is, create an object of type Tile and assign it to a particular element in tiles.

Tile myTile;

// do something with myTile

this->tiles[0] = myTile;

2 Comments

Well, I need to call the constructor of Tile with some arguments, i.e. I need to do Tile myTile = new Tile(...) which won't work: error: conversion from 'Tile*' to non-scalar type 'Tile' requested --- coming from Java btw :)
@Ben: I understand you had background on C# or Java :). For your case, simply use this->titles[0] = Tile(...) ;)
0

new Tiles() returns a pointer to a Tiles instance.

Tile *tiles defines an array out Tiles, not pointers.

Start with Tile **tiles instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.