首页 \ 问答 \ 对'Class :: Class'的未定义引用(Undefined reference to 'Class::Class')

对'Class :: Class'的未定义引用(Undefined reference to 'Class::Class')

解决了之前的问题后(​​请参阅我的其他问题)。 我已经宣布了更多的课程。

其中之一被称为CombatAdmin,它做各种事情:(头文件)

#ifndef COMBATADMIN_H
#define COMBATADMIN_H

#include <string> // Need this line or it complains
#include <Player.h>
#include <Sound.h>
#include <Enemy.h>
#include <Narrator.h>
using namespace std;

class Enemy;
class Player;

class CombatAdmin // Code yet to be commented here, will come soon.
{
    public:
        CombatAdmin();
        void healthSet(double newHealth, string playerName);
        void comAdSay(string sayWhat);
        void playerFindsChest(Player *player,Weapon *weapon,Armour *armour);
        void youStoleOurStuffEncounter(Player *player);
        void comAdWarning(string enemyName);
        void comAdAtkNote(string attack, double damage,string target,string aggresor);
        void entDefeated(string entName);
        void comAdStateEntHp(string ent, double hp);
        void comAdStateScanResults(string enemyName, double enemyHealth);
        string doubleToString(double number);
        string intToString(int number);
        bool isRandEncounter();
        void randomEncounter(Player *player,Sound *sound,Narrator *narrator);
        bool combatRound(Player *player, Enemy *enemy, Sound *sound, bool ran);
        void playerFindsItem(string playerName,string itemName,double itemWeight,double playerWeight);
        void playerFindsGold(string playerName,double coinCnt,double playerCoinCnt);

};

#endif // COMBATADMIN_H

然后在main.cpp文件中实例化,如下所示:(main.cpp文件的片段)

#include <iostream> // Required for input and output
#include <Item.h> // Item header file.
#include <Weapon.h> // Header files that I have made for my classes are needed for this program
#include <sstream> // Needed for proper type conversion functions
#include <windows.h> // for PlaySound() and other functions like sleep.
#include <time.h> // Needed to seed the rand() function.
#include <mmsystem.h> // Not sure about this one, possibly defunct in this program.
#include <stdio.h> // Needed for a similar kind of output as iostream for various functions error msgs.
#include <irrKlang.h> // The header file of the sound lib I am using in this program.
#include <Narrator.h> // The narrators's header file.
#include <Pibot.h> // Other header files of classes.
#include <Armour.h>
#include <Player.h>
#include <Weapon.h>
#include <CombatAdmin.h>

using namespace irrklang;

using namespace std;

// Forward referenced functions


void seedRandom(); // Seeds the random number so it will be random as apposed to pseudo random.
string getPlayerName(string temp); // Gets the player's new name.


int main(int argc, char* argv[])
{
    // Variables and object pointers declared here.
    CombatAdmin *comAd = new CombatAdmin(); // Handles combat.
    Narrator *narrator = new Narrator(); // The Narrator that says stuff
    Pibot *piebot = new Pibot(); // PIbot, the player's trusty companion

    string temp; // Temp string for input and output

但是,当我尝试编译该项目时,出现以下错误:

C:\Documents and Settings\James Moran.HOME-B288D626D8\My Documents\C++ projects\Test Project\main.cpp|59|undefined reference to `CombatAdmin::CombatAdmin()'|

我正在使用GNU GCC编译器的Code :: Blocks IDE(版本10.05)。 该项目是“控制台应用程序”类型。 我正在使用Windows XP 32位SP3。

我试图改变搜索目录来包含目标文件的位置,但没有成功。

从代码中可以看出,叙述者和“PIbot”实例很好。 (然后使用,未示出)

因此,我的问题是,我需要做些什么来阻止这些错误的发生? 就像我在使用库之前遇到类似的“Undefined reference to x”错误一样。 我刚刚忘记了在Code :: Blocks中链接到它们,只要我这样做,它们就会工作。

由于这个班是我自己制作的,我对此不太确定。

请说明是否需要更多关于代码等的信息


After fixing the previous problem (see my one other question that I have asked). I had declared more classes.

One of these is called CombatAdmin which does various things: (Header file)

#ifndef COMBATADMIN_H
#define COMBATADMIN_H

#include <string> // Need this line or it complains
#include <Player.h>
#include <Sound.h>
#include <Enemy.h>
#include <Narrator.h>
using namespace std;

class Enemy;
class Player;

class CombatAdmin // Code yet to be commented here, will come soon.
{
    public:
        CombatAdmin();
        void healthSet(double newHealth, string playerName);
        void comAdSay(string sayWhat);
        void playerFindsChest(Player *player,Weapon *weapon,Armour *armour);
        void youStoleOurStuffEncounter(Player *player);
        void comAdWarning(string enemyName);
        void comAdAtkNote(string attack, double damage,string target,string aggresor);
        void entDefeated(string entName);
        void comAdStateEntHp(string ent, double hp);
        void comAdStateScanResults(string enemyName, double enemyHealth);
        string doubleToString(double number);
        string intToString(int number);
        bool isRandEncounter();
        void randomEncounter(Player *player,Sound *sound,Narrator *narrator);
        bool combatRound(Player *player, Enemy *enemy, Sound *sound, bool ran);
        void playerFindsItem(string playerName,string itemName,double itemWeight,double playerWeight);
        void playerFindsGold(string playerName,double coinCnt,double playerCoinCnt);

};

#endif // COMBATADMIN_H

It is then instanced in the main.cpp file like this: (Snippet of the main.cpp file)

#include <iostream> // Required for input and output
#include <Item.h> // Item header file.
#include <Weapon.h> // Header files that I have made for my classes are needed for this program
#include <sstream> // Needed for proper type conversion functions
#include <windows.h> // for PlaySound() and other functions like sleep.
#include <time.h> // Needed to seed the rand() function.
#include <mmsystem.h> // Not sure about this one, possibly defunct in this program.
#include <stdio.h> // Needed for a similar kind of output as iostream for various functions error msgs.
#include <irrKlang.h> // The header file of the sound lib I am using in this program.
#include <Narrator.h> // The narrators's header file.
#include <Pibot.h> // Other header files of classes.
#include <Armour.h>
#include <Player.h>
#include <Weapon.h>
#include <CombatAdmin.h>

using namespace irrklang;

using namespace std;

// Forward referenced functions


void seedRandom(); // Seeds the random number so it will be random as apposed to pseudo random.
string getPlayerName(string temp); // Gets the player's new name.


int main(int argc, char* argv[])
{
    // Variables and object pointers declared here.
    CombatAdmin *comAd = new CombatAdmin(); // Handles combat.
    Narrator *narrator = new Narrator(); // The Narrator that says stuff
    Pibot *piebot = new Pibot(); // PIbot, the player's trusty companion

    string temp; // Temp string for input and output

However, when I try to compile the project, I get the following error:

C:\Documents and Settings\James Moran.HOME-B288D626D8\My Documents\C++ projects\Test Project\main.cpp|59|undefined reference to `CombatAdmin::CombatAdmin()'|

I am using the Code::Blocks IDE (ver 10.05), with the GNU GCC compiler. The project is of type "Console application". I am using windows XP 32 bit SP3.

I have tried changing to search directories to include where the object files are, but no success there.

As can be seen from the code, the narrator and "PIbot" are instanced just fine. (then used, not shown)

My question is, therefore, what do I need to do to stop these errors occurring? As when I encountered similar "Undefined reference to x" errors before using libraries. I had just forgotten to link to them in Code::Blocks and as soon as I did, they would work.

As this class is of my own making I am not quite sure about this.

Do say if you need more information regarding the code etc.


原文:https://stackoverflow.com/questions/5393293
更新时间:2023-08-31 15:08

最满意答案

创建一个地图数组,如下所示:

var maps = []; // array for now
var geocoder;

$(document).ready(function(){
    google.maps.event.addDomListener(window, 'load', initialize('map_1')); // two calls
    google.maps.event.addDomListener(window, 'load', initialize('map_2'));
});

function initialize(_id) { // Map id for future manipulations
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
        zoom: 16,
        center: new google.maps.LatLng(50.317408,11.12915),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    maps[_id] = new google.maps.Map(document.getElementById('map_canvas_'+_id), // different containers
        mapOptions);
        codeAddress($('#entityID span#address').text());
}

function codeAddress(address, map_id) {

    geocoder.geocode( {
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: maps[map_id], // target nedded map by second parametr
                position: results[0].geometry.location
            });
        } else {
            codeAddress('germany');
        }
    });
}

Create an array of maps, something like this:

var maps = []; // array for now
var geocoder;

$(document).ready(function(){
    google.maps.event.addDomListener(window, 'load', initialize('map_1')); // two calls
    google.maps.event.addDomListener(window, 'load', initialize('map_2'));
});

function initialize(_id) { // Map id for future manipulations
    geocoder = new google.maps.Geocoder();
    var mapOptions = {
        zoom: 16,
        center: new google.maps.LatLng(50.317408,11.12915),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    maps[_id] = new google.maps.Map(document.getElementById('map_canvas_'+_id), // different containers
        mapOptions);
        codeAddress($('#entityID span#address').text());
}

function codeAddress(address, map_id) {

    geocoder.geocode( {
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: maps[map_id], // target nedded map by second parametr
                position: results[0].geometry.location
            });
        } else {
            codeAddress('germany');
        }
    });
}

相关问答

更多
  • 也许只是最后一个initialize函数在浏览器中触发? 尝试只使用1个初始化函数来初始化所有地图。 Perhaps just the last initialize function is firing in the browser? Try having just 1 initialise function that initialises all your maps.
  • 是的,您的directionsService会在循环中被覆盖。 您可以做的是将请求链接起来。 因此,每当响应返回时,您都会触发下一个请求。 Javascript, multiple google maps on same page(Directions Renderer is not working as excepted) 不要在任何其他地 ...
  • 要添加多个地图,只需添加另一个具有不同名称属性的cfmap标记。 以下代码在我的9.0.1系统上运行正常。
  • 创建一个地图数组,如下所示: var maps = []; // array for now var geocoder; $(document).ready(function(){ google.maps.event.addDomListener(window, 'load', initialize('map_1')); // two calls google.maps.event.addDomListener(window, 'load', initialize('map_2')); } ...
  • 您也可以下载KML等地图,下载网址类似 https://www.google.com/maps/d/kml?mid=themymaps.mapid 它没有记录,但是当我使用这个URL创建一个KmlLayer的时候它现在对我很有用(只要它们没有修改下载过程,它就会工作)。 示例( 原始地图 ) function initialize() { var map = new google.maps.Map(document.getElementById('map-canvas'), { zo ...
  • 创建一个可能看起来像这样的全局函数(可能在app/assets/application.js ): function addShopMap(shop_id, lat, lng, direction){ var mapOptions = { zoom: 3 }; var map = new google.maps.Map(document.getElementById('map-canvas-'+shop_id), mapOptions); ...
  • 尝试 try