-7

I just want to prompt some integer from the user and pass it to the constructor function

Here's my class:

class queen {
 int N;
 int** queens;
 int *BFSq;
 int s;
 int front = 0;
 int rear = -1;
 int *found;

And here's my constructor:

public:
queen(){};
queen(int n)
{
    N=n;
    queens = new int*[n];
    for(int i = 0; i < n; ++i)
        queens[i] = new int[n];
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            queens[i][j]=0;
        }
    }
     BFSq = new int[n];
    found = new int[n];
}

And here is my main()function:

int main(){
int ent = 0;
cout<<"Please enter the size of your board\n";
cin>>ent;
queen(ent);
 }

I just want to pass ent value as int n argument to my constructor,but i think I'm not doing it right because I'm getting errors

EDIT: the error is "redefinition of 'ent' with a different type:'queen' vs 'int' "

pouyan021
  • 154
  • 1
  • 6
  • 15
  • What are the errors? Are those constructors inside the class definition? It not you need `queen::queen()` and `queen::queen(int n)` instead – Kevin Nov 13 '15 at 21:38
  • We need all the code. E.g. `queen.h`. – Luke Joshua Park Nov 13 '15 at 21:39
  • Please include more code so we can see how the class is constructed. – breen Nov 13 '15 at 21:39
  • Please edit your question to include that so it can be formatted properly – Kevin Nov 13 '15 at 21:45
  • @Kevin i just added some code is there any point about my question which is not clear to you? – pouyan021 Nov 13 '15 at 21:51
  • @Kevin `queen::queen(int n)` doesn't work – pouyan021 Nov 13 '15 at 21:56
  • Because the constructors were defined in the class definition. As I said, you only need to qualify the class if the constructors are outside the class definition. I made that recommendation before you showed the full definition – Kevin Nov 13 '15 at 21:57
  • Possible duplicate of [Call constructor from constructor in C++](http://stackoverflow.com/questions/308276/call-constructor-from-constructor-in-c) – pouyan021 Dec 24 '15 at 13:06

1 Answers1

1

In main, queen(ent);, rather than creating a temporary queen using the constructor taking an int, tries to create a queen object called ent using the default constructor. Since ent already exists as an int the compiler throws an error. The solution is to create the object like this: queen q(ent);

Kevin
  • 6,483
  • 1
  • 14
  • 23
  • doesn't seems to work also `ent` in `queen(ent)` doesn't refer to an object and even the compiler knows it when you just type that function,it knows that it supposed to be an integer – pouyan021 Nov 13 '15 at 22:03
  • What is the error you're having then? I'm stuck using an older version of g++ at the moment so my error might not be the same as yours. Edit it into your question – Kevin Nov 13 '15 at 22:04
  • That's the same error as I had. Please replace `queen(ent);` with `queen q(ent);` and tell me if it fixes the issue – Kevin Nov 13 '15 at 22:07