0

Ive been writing my code and have declared and defined all the necessary variables and included the necessary header files. Still it gives me "unresolved external symbol" on any functions used within the classes constructor.

BowlingAlley.obj : error LNK2001: unresolved external symbol "private: void __thiscall     BowlingAlley::addToMasterArray(int * const,int * const)" (?addToMasterArray@BowlingAlley@@AAEXQAH0@Z)
1>BowlingAlley.obj : error LNK2001: unresolved external symbol "private: void __thiscall     BowlingAlley::createLaneArrays(void)" (?createLaneArrays@BowlingAlley@@AAEXXZ)
1>BowlingAlley.obj : error LNK2001: unresolved external symbol "int * laneVertices" (?laneVertices@@3PAHA)

My header file is as follows.

#pragma once

#include <stdio.h>
#include <iostream>
#include <array>



class BowlingAlley
{
private: 
    void createLaneArrays();
    void concatenateArrays(int array1[], int array2[], int containerArray[]);
    int getNewArrayLength(int array1[], int array2[]);
    //mutates an array creating the master draw array by using the two functions above
    void addToMasterArray( int array2[], int masterArray[]);

public:
        BowlingAlley(int width, int gHeight, int length);
    BowlingAlley();

};

and the implementation looks like this

    #include "BowlingAlley.h"
#include <stdio.h>
#include <GL/glut.h> 
#include <math.h>
#include <iostream>

using namespace std;
//BowlingAlley class contains all of the vertex information for the alley and the backdrop itself




//the lane's vars
int laneVertices[];
int laneIndices[];
int alleyArray[];

int alleyLength;
int alleyWidth;
int guardHeight;



BowlingAlley::BowlingAlley(int width, int gHeight, int length)
{
    alleyLength = length;
    alleyWidth = width;
    guardHeight = gHeight;
    createLaneArrays();
    //the masterdrawarray doesn't exist yet, create it with size 0
    int* masterDrawArray = new int[0];
    //start adding the vert Arrays to the master array
    addToMasterArray(laneVertices, masterDrawArray);

}

void createLaneArrays()
{
    float laneVertices[]= {
        -alleyWidth/2,0,0, 
        alleyWidth/2,0,0,
        -alleyWidth/2,0,alleyLength,
        alleyWidth/2,0,alleyLength
    };

    float laneIndices[]= {0,1,2,1,2,3};
}

int getNewArrayLength(int array1[], int array2[])
{
    //the number of elements in arrays1/2
    float array1Size = sizeof(array1)/sizeof(int*);
    float array2Size = sizeof(array2)/sizeof(int*);
    //the length of the new array
    int newArrayLength = array1Size + array2Size; 
    //return it
    return newArrayLength;
}

void concatenateArrays(int array1[], int array2[], int containerArray[])
{
    //the number of elements in arrays1/2
    float array1Size = sizeof(array1)/sizeof(int);
    float array2Size = sizeof(array2)/sizeof(int);
    //the length of the new array
    int newArrayLength = array1Size + array2Size; 

    //loop through adding array1 to the new array
    for (int i = 0; i<array1Size; i++)
    {
        containerArray[i] = array1[i];
    }
    //loop through adding array2 to the new array
    for (int i = array1Size; i<newArrayLength; i++)
    {
        int j = i - array1Size;
        containerArray[i] = array2[j];
    }




}

void addToMasterArray(int array2[], int masterArray[])
{
    //create a temporary array to act as a kind of buffer whil we delete and reset the size of the drawing array
    int* tempArray = new int[(sizeof(masterArray)/sizeof(int))];
    //copy the contents over to the temp array.
    for(int i =0; i<(sizeof(masterArray)/sizeof(int)); i++)
    {
        tempArray[i] = masterArray[i];
    }

    //delete what drawingArray points to
    delete[] masterArray;
    //calculate the size of the new array
    int arrLength = getNewArrayLength(array2, masterArray);
    //resurrect a new and extended drawingArray from the ashes, big enough to accomodate both arrays.
    masterArray = new int[arrLength];
    //use our previously made concatenateArrays function to append the two arrays
    concatenateArrays(tempArray, array2, masterArray);

    cout <<"The number of elements in the Master Array is: "<< sizeof masterArray/sizeof masterArray[0]<<endl;


}

The object is then created and the function called in main in another .cpp file.

#include "BowlingAlley.h"
#include <stdio.h>
#include <GL/glut.h> 
#include <math.h>
#include "imageloader.h"
#include "BowlingAlley.h"

    ////////////////////////////////////////////// 
int main(int argc, char** argv) 
{
  glutInit(&argc, argv);    // GLUT Initialization 
  glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE); // Initializing the Display mode
  glutInitWindowSize(800,600);  // Define the window size
  glutCreateWindow("Taken Bowl");   // Create the window, with caption.
        printf("\n========== McLeanTech Systems =========\nBecoming Sentient");
  init();   // All OpenGL initialization

 BowlingAlley* myBowlingAlley = new BowlingAlley(100,30,1000); //make us a bowling alley


  //-- Callback functions ---------------------
  glutDisplayFunc(display);
  glutKeyboardFunc(mykey);
  //glutMouseFunc(mymouse);
  glutSpecialFunc(processSpecialKeys);  

  glutMainLoop();   // Loop waiting for event 
}

I just cannot see what I'm leaving out.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34

2 Answers2

6

The "unresolved" methods are not methods at all. You left out the class name.

Change:

void createLaneArrays()

to:

void BowlingAlley::createLaneArrays()

and do the same for the other 2. Then they will be part of the class. They are not automatically in the class just because they are in the same file as in C# or Java.

Mark Stevens
  • 2,366
  • 14
  • 9
  • Thank you, very understandable. This caught me off guard because I've used C++ before, however it was large, messy code all in one .cpp file. However, I've tack on BowlingAlley:: to the front of the methods in both the .h and .cpp files, however the ARRAY is still telling me it's unresolved. Must I do the same (prefixing BowlingAlley::) for the array declarations? – Guy Joel McLean Oct 18 '12 at 23:34
  • The basic answer is "yes", you probably want all data and code to be prefixed with the class name - to be part of the class. I see that you have a file-level array called "laneVertices[]" thats of type int, and one in "createLaneArrays" with the exact same name but a different data type. Generally, member variable are prefixed with "m_" or similar to keep them straight - for both the compiler and the programmer. Also, the arrays "created" in createLaneArrays() are just allocated and then disappear when the function returns. I'm not sure that's what you had in mind. – Mark Stevens Oct 18 '12 at 23:57
  • Yes i have noticed that and amended it. They're meant to both be of type float. I'm attempting to create the arrays on the class level using "new". laneVertices = new float[] (within createLaneArrays()) But for some reason the compiler is now giving me that "laneVertices must be a modifiable Ivalue. I thought that was how you defined (new) arrays. – Guy Joel McLean Oct 19 '12 at 12:26
  • You're on the right track allocating class level members with "new". I don't know what the lvalue is in your new code, maybe you have something like "float a[4] = new float[4]"? What you need is "float* m_pArray;" defined at the class level and "m_pArray = new float[4];" in the code. "new" will return a pointer so the expression on the left of the '=' (the lvalue) needs to be a pointer type. – Mark Stevens Oct 19 '12 at 15:34
3

Because you're implementing those function without BowlingAlley:: prefix.

This way you create new set of functions (not methods of your class) and there're missing implementations then.

void createLaneArrays()
{
    float laneVertices[]= {
    // ...

Should be:

void BowlingAlley::createLaneArrays()
{
    float laneVertices[]= {
    // ...

And the same for each of them.

And you have another error in createLaneArrays(), where you initialize local variable laneVertices[] instead of using class variable.

You probably want to:

laneVertices = new float[6];

And don't forget to delete in destructor (or detroy like function):

delete [] laneVertices;
Vyktor
  • 20,559
  • 6
  • 64
  • 96