0

I'm a student who's working on an tourism guide program using xcode. I'm trying to set the object Ds of type Sport which is a dynamic array (I think so) to be of type Attraction. Then I want to pass the new Attraction object to the setAttraction() function, but I get the error:

"cannot initialize a parameter of type 'Attraction**' to a lvalue of type 'Attraction*'.

Now I am new to OOP and C++ so I don't what that means.

main.cpp:

int main() {
    int x;
    City Rome(111,"Rome");
    City Dubai(222,"Dubai");
    City Paris(333, "Paris");

    cin >> x;

    Sport* Ds = new Sport[x];
    Culture* Dc = new Culture[x];
    Shopping* Dh = new Shopping[x];

    //{ new Sport, new Sport, new Sport, new Culture, 
    //  new Culture, new Culture, new Shopping, new Shopping, new Shopping};

    Ds[0].setName("Camel and Horse Racing");
    Ds[1].setName("Marine Sports");
    Ds[2].setName("Football");
    Dc[0].setName("Dubai Museum");
    Dc[1].setName("Falconry");
    Dc[2].setName("Sheikh Saeed Al-Maktoum's House");
    Dh[0].setName("Dubai Mall");
    Dh[1].setName("Mall of Emirates");
    Dh[2].setName("Deira City Centre");
    Ds[0].setIDType(1);
    Ds[1].setIDType(1);
    Ds[2].setIDType(1);
    Dc[0].setIDType(2);
    Dc[1].setIDType(2);
    Dc[2].setIDType(2);
    Dh[0].setIDType(3);
    Dh[1].setIDType(3);
    Dh[2].setIDType(3);

    //Here's the problem
    Attraction* z = new Attraction[x];
    *z = *Ds;

    Dubai.setAttraction(z, x);
    menu(Rome,Dubai,Paris);

    return 0;
}

'City.cpp':

#include "City.h"
#include "Sport.h"
#include "Culture.h"
#include "Shopping.h"
#include <iostream>
#include "Attraction.h"
using namespace::std;

string City::getName(){
    return name;
}

int City::getID(){
    return id;
}

void City::setAttraction(Attraction a[], int size){
    attractions = new Attraction[size];
    for (int i = 0; i < size ; i++) {
        attractions[i].setName(a[i].getName());//Here's the thread
        cout << attractions[i].getName() << endl;
    }
    for (int j = 0; j < size ; j++) {
        attractions[j].setIDType(a[j].getIDType());
    }
}

void City::displayAttraction(){
    cout << "There you go.\n\n";
    for (int i = 0; i < 9 ; i++) {
        cout << attractions[i].getName();
    }
}
  • Please read how to create a Minimal, Complete, and Verifiable example :http://stackoverflow.com/help/mcve – Ziezi Dec 20 '15 at 13:40
  • Do `Sports`, `Culture`, `Shopping` inherit `Attraction` class? – user007 Dec 20 '15 at 13:50
  • @simplicisveritatis I'm sorry, but can you tell me how is the question not minimal, complete, or verifiable, especially after you edited it, which I thank you for. The code is too long, so this is the best I can do to show the problem. I also highlighted the location of the problems in the code. – Mahmoud Dawlatly Dec 20 '15 at 13:53
  • I edited your question to make it more presentable and easy to read. Regarding the rest of your comment, you obviously haven't read anything from the link I've posted. – Ziezi Dec 20 '15 at 13:54
  • @simplicisveritatis Thank you. – Mahmoud Dawlatly Dec 20 '15 at 13:56

1 Answers1

2

The problem is your declaration of the function setAttraction the declaration of the setAttraction is like this ::

void setAttraction(Attraction* a[], int size)

Since you have defined Attraction *a[] it means a is a double pointer, which is why you are getting the error, since you pass a single pointer argument to the function z when you call it. So, you either change the declaration to something like this ::

void setAttraction(Attraction* a, int size);

or

 void setAttraction(Attraction a[], int size);

I think this should resolve it.

Moreover in your code you do this ::

Attraction* z = new Attraction[x];
*z = *Ds;

I think you are trying to create an array of Attraction and then copy the Sport array to z. But, indeed you are just copying the first object in array Ds to the first object of z, and not the whole array. Since you are assigning the objects of derived class to base class, this will eventually lead to Slicing problem. What is object slicing?

A better approach would be simply passing the pointer Ds to the function setAttraction (Since the first parameter of setAttraction is the base class pointer, so it will work). Something like this ::

Dubai.setAttraction(Ds, x);

And in the base class Attraction defining the function getName as virtual, so that it can be used on any type of Attraction (Sport, Culture, Shopping, etc)

Community
  • 1
  • 1
user007
  • 2,156
  • 2
  • 20
  • 35
  • Well, I've done what you wrote except the virtual function, as it produced another thread**thread 1 exc_bad_access (code=1 address=0x0)**. Yet, still i'm only getting the first object of the array. – Mahmoud Dawlatly Dec 24 '15 at 12:02
  • @MahmoudDawlatly Are you passing `Ds` to `setAttraction()`?? I don't think that is possible, or probably there is some other code! And how do you know that you only get the first object?? Can you show the complete code? Probably copy/paste all the code and classes to ideone.com or pastebin.com – user007 Dec 24 '15 at 21:10
  • @MahmoudDawlatly I wonder if there is some problem in inheritance, which is leading to the `thread 1 exc_bad_access (code=1 address=0x0)` error – user007 Dec 24 '15 at 21:12
  • No, I'm pretty sure about the inheritance structure. But anyways, I've decided to switch to using vectors, which actually is much easier. – Mahmoud Dawlatly Dec 26 '15 at 06:34