C++ 为什么我的代码会生成一个";未解析的外部符号“;错误?
我一直在写代码,声明并定义了所有必要的变量,包括必要的头文件。但它在类构造函数中使用的任何函数上都给了我“未解析的外部符号”C++ 为什么我的代码会生成一个";未解析的外部符号“;错误?,c++,oop,class,unresolved-external,C++,Oop,Class,Unresolved External,我一直在写代码,声明并定义了所有必要的变量,包括必要的头文件。但它在类构造函数中使用的任何函数上都给了我“未解析的外部符号” BowlingAlley.obj : error LNK2001: unresolved external symbol "private: void __thiscall BowlingAlley::addToMasterArray(int * const,int * const)" (?addToMasterArray@BowlingAlley@@AAEXQA
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();
};
#pragma一次
#包括
#包括
#包括
保龄球
{
私人:
void createLanArrays();
无效连接阵列(国际阵列1[],国际阵列2[],国际集装箱阵列[]);
int-getNewArrayLength(int-array1[],int-array2[]);
//使用上述两个函数修改数组,创建主绘制数组
void addToMasterArray(int-array2[],int-masterArray[]);
公众:
保龄球尺(内宽、内高、内长);
鲍林加里();
};
实现过程如下所示
#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;
}
#包括“BowlingAlley.h”
#包括
#包括
#包括
#包括
使用名称空间std;
//BowlingAlley类包含球道和背景本身的所有顶点信息
//这条小路上有辆车
国际旅行社[];
国际laneIndices[];
国际航空公司[];
内联长度;
国际胡同宽度;
内卫八;
BowlingAlley::BowlingAlley(int-width、int-gHeight、int-length)
{
alleyLength=长度;
小巷宽度=宽度;
卫兵八号=八号;
CreateLanArrays();
//masterdrawarray尚不存在,请创建大小为0的数组
int*masterDrawArray=newint[0];
//开始将垂直阵列添加到主阵列
addToMasterArray(laneVertices、masterDrawArray);
}
void createLanArrays()
{
浮动车道[]={
-胡同宽度/2,0,0,
胡同宽度/2,0,0,
-胡同宽度/2,0,胡同长度,
胡同宽度/2,0,胡同长度
};
浮点LaneIndicates[]={0,1,2,1,2,3};
}
int-getNewArrayLength(int-array1[],int-array2[]
{
//阵列中的元素数为1/2
float array1Size=sizeof(array1)/sizeof(int*);
float array2Size=sizeof(array2)/sizeof(int*);
//新数组的长度
int newArrayLength=array1Size+array2Size;
//还它
返回newArrayLength;
}
无效连接阵列(内部阵列1[],内部阵列2[],内部容器阵列[])
{
//阵列中的元素数为1/2
float array1Size=sizeof(array1)/sizeof(int);
float array2Size=sizeof(array2)/sizeof(int);
//新数组的长度
int newArrayLength=array1Size+array2Size;
//循环将数组1添加到新数组
对于(int i=0;i,因为实现这些函数时没有BowlingAlley::
前缀
通过这种方式,您可以创建一组新的函数(而不是类的方法),然后就会缺少实现
void createLaneArrays()
{
float laneVertices[]= {
// ...
应该是:
void BowlingAlley::createLaneArrays()
{
float laneVertices[]= {
// ...
他们每个人都一样
在createLanArrays()
中还有另一个错误,在这里您初始化局部变量laneVertices[]
,而不是使用类变量
您可能想:
laneVertices = new float[6];
不要忘记在(或detroy
like函数)中删除:
“未解析”方法根本不是方法。您遗漏了类名
更改:
void createLaneArrays()
致:
和其他2个相同的。然后它们将是类的一部分。它们不是自动地在类中,因为它们在C++或java中的文件是相同的。
您在代码中定义了成员函数。不管它是一个大的、凌乱的代码还是一个.cpp文件。但是,我在.h和.cpp文件中的方法前面加上了BowlingAlley::,但是数组仍然告诉我它没有解析。我必须对数组声明做同样的操作(前缀BowlingAlley::)吗?基本答案是“是”,您可能希望所有数据和代码都以类名作为前缀-作为类的一部分。我看到您有一个名为“laneVertices[]”的文件级数组,该数组的类型为int,而在“createlanarrays”中有一个数组的名称完全相同,但数据类型不同。通常,成员变量的前缀为“m”或者类似的方法来保持它们的直截了当-对于编译器和程序员都是如此只是分配了,然后在函数返回时消失。我不确定您是否想到了这一点。是的,我注意到了这一点,并对其进行了修改。它们都是float类型。我正在尝试使用“new”在类级别上创建数组。laneVertices=new float[](在createLanArrays()内)但出于某种原因,编译器现在告诉我“laneVertices必须是可修改的Ivalue。我认为这就是您定义(新)数组的方式。您使用“new”分配类级成员的方法是正确的。我不知道新代码中的左值是什么,也许您有类似“float a[4]=new float[4]”的东西?您需要的是float*m_pArray;“在类级别定义”和代码中的“m_pArray=new float[4]”。“new”将返回指针,因此“=”(左值)左侧的表达式需要是指针类型。
delete [] laneVertices;
void createLaneArrays()
void BowlingAlley::createLaneArrays()