-3

I am having an issue with my function call

static classname *makeclass ( char ch, int x1, int y1, int x2, int y2){

    cin >> ch >> x1 >> y1 >> x2 >> y2;
    Ship **ptr = 0;
    if ( ch != 'A' || ch != 'B' || ch != 'C' || ch  != 'D'){
            throw invalid_argument ("Error : invalid character input ");
    }

    else if ( ch == 'A'){
            classname **ptr = new derivedclassname*[5];
            for ( int i = 0 ; i < 5; i++){
                    ptr[i] = new derivedclass (x1,y1,x2,y2);
            }
            return *ptr;
    }

    return *ptr;

}

In my main file :

main () {
 classname *p = classname::*makeclass ( x1,y1,x2,y2);

}

My error:

Ship.cpp:81:14: warning: ‘classname* makeclass(char, int, int, int, int)’ defined but not      used [-Wunused-function]
static classname *makeclass ( char ch, int x1, int y1, int x2, int y2){
          ^
/tmp/cc1eUoXA.o: In function `main':
testclass.cpp:(.text+0xc1): undefined reference to `classname::makeclass(char, int, int, int, int)'
Zong
  • 6,160
  • 5
  • 32
  • 46
  • Notwithstanding the various issues with the syntax, why are you passing parameters to the `makeclass` function since they are passed by value and filled by the `cin` call within ? – SirDarius May 12 '14 at 19:13
  • `static` looks wrong, unless you have this inline in a class declaration! Otherwise the linker sees this only private for the compilation unit it was defined in. – πάντα ῥεῖ May 12 '14 at 19:16

2 Answers2

0

This code has a lot of issues.

static classname *makeclass ( char ch, int x1, int y1, int x2, int y2){

This function should not be static, since, according to the information you provided, it is not located in the same file as the main function.

Also, you don't even need to pass these parameters to begin with, because of the next line:

    cin >> ch >> x1 >> y1 >> x2 >> y2;

Everyone of these should be declared locally in the function.

    Ship **ptr = 0;

This pointer to pointer variable seems to be shadowed by a variable of the same name declared later... This should be avoided at all costs.

    if ( ch != 'A' || ch != 'B' || ch != 'C' || ch  != 'D'){
            throw invalid_argument ("Error : invalid character input ");
    }

So, ch is valid if it has at the same time the values 'A', 'B', 'C', and 'D'. As it is, your function will always throw an exception...

    else if ( ch == 'A'){
            classname **ptr = new derivedclassname*[5];

This declaration shadows a previously declared variable.

            for ( int i = 0 ; i < 5; i++){
                    ptr[i] = new derivedclass (x1,y1,x2,y2);
            }
            return *ptr;

Let's suppose you meant derivedclassname instead of derivedclass. Here you return a dereferenced pointer to the first pointer, meaning you only correctly return the address of the first classname you have created.
The four following ones are lost, and you have created a memory leak.

    }

    return *ptr;

Ah, the best part comes last, you are dereferencing a pointer with a 0 value, which is undefined behaviour and will probably crash your program.

}

Finally, your main should look like:

main () {
    classname *p = makeclass ( x1,y1,x2,y2);
}

since you are simply calling a function. A function that does NOT belong to a class.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
0

If you are still working on the problem from https://stackoverflow.com/questions/23601774/class-factory-returns-a-pointer-to-the-instance-of-a-new-derived-class, you need to define the function as:

classname* classname::makeclass ( char ch, int x1, int y1, int x2, int y2){
// ....
}
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270