c++ - 为什么我的代码会生成 "Unresolved external symbol"错误?

标签 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 
}

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

最佳答案

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

改变:

void createLaneArrays()

到:

void BowlingAlley::createLaneArrays()

对另外两个做同样的事情。然后他们将成为类(class)的一部分。它们不会因为与 C# 或 Java 位于同一文件中而自动在类中。

关于c++ - 为什么我的代码会生成 "Unresolved external symbol"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961925/

相关文章:

php - 我不明白为什么DeleteTask.php不起作用,但RetrieveTask.php却起作用

C++ 可以发现错误

c++ - 书籍示例在类声明中使用 "explicit"关键字。无效吗?

c++ - 添加我自己的编译器警告

c++ - 将 const char 传递给不兼容的类型 double

c++ - 从其基类捕获的自己的异常类调用方法

C++ 动态库段错误

php - 转换为 PDO

oop - 面向服务与面向对象——它们可以共存吗?

Java:比较两个类列表,无论顺序如何