0

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

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 
}

我只是看不到我遗漏了什么。

4

2 回答 2

6

“未解决”的方法根本不是方法。你漏掉了类名。

改变:

void createLaneArrays()

到:

void BowlingAlley::createLaneArrays()

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

于 2012-10-18T19:09:18.087 回答
3

因为您正在实现那些没有BowlingAlley::前缀的功能。

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

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

应该:

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

他们每个人都一样。

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

您可能想要:

laneVertices = new float[6];

并且不要忘记在析构函数(或detroy类似函数)中删除:

delete [] laneVertices;
于 2012-10-18T19:08:55.790 回答