为什么我的代码生成“未解析的外部符号”错误?

时间:2012-10-18 19:06:10

标签: c++ oop class unresolved-external

我一直在编写代码并声明并定义了所有必需的变量并包含了必要的头文件。它仍然在类构造函数中使用的任何函数上给出了“未解析的外部符号”。

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)

我的头文件如下。

#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();

};

,实现看起来像这样

    #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;


}

然后创建该对象,并在另一个.cpp文件中调用main函数。

#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 
}

我只是看不出我要遗漏的东西。

2 个答案:

答案 0 :(得分:6)

“未解决”的方法根本不是方法。你遗漏了班级名称。

变化:

void createLaneArrays()

为:

void BowlingAlley::createLaneArrays()

并为其他人做同样的事情2.然后他们将成为班级的一部分。它们不会自动类中,因为它们与C#或Java位于同一文件中。

答案 1 :(得分:3)

因为您在没有BowlingAlley::前缀的情况下实现了这些功能。

这样你就可以创建新的函数集(不是你的类的方法),然后就会缺少实现。

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

应该是:

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

每个人都一样。

你在createLaneArrays()中有另一个错误,你初始化局部变量laneVertices[]而不是使用类变量。

你可能想:

laneVertices = new float[6];

不要忘记在destructor(或detroy类似功能)中删除:

delete [] laneVertices;