简体   繁体   English

返回类指针C ++

[英]Returning class pointer C++

I have window_types.cpp 我有window_types.cpp

#include <string>
#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"

#include "../../utils/utilsf.h"
#include "../../extra_data/extra_data.h"

#include "window.h"
#include "window_types.h"


using namespace std;
using namespace windows;



message_window::message_window(string title,string con,int a[],int b[]) : bwindow(title, 300, 200, 300, 200,a,b){
    vector <string> content_ordered;
    string new_lines = "";

    for (int x = 0;x < con.size();x++){
        if (utilsf::cts(con[x]) == "/" and utilsf::cts(con[x]) == "r"){
            content_ordered.push_back(new_lines);
            new_lines = "";
            x+=2;
        }
        new_lines = new_lines + utilsf::cts(con[x]);
    }

    SDL_Color textColor = {0, 0, 0};

    TTF_Font *font = fonts::load_john_han(15);
    int h = 0;
    for (int x = 0;x < content_ordered.size();x++){

        SDL_Surface* text_surface =  TTF_RenderText_Solid(font,content_ordered[x].c_str(),textColor);

        apply_surface(5,h,text_surface,content);
        h += text_surface->h + 3;
    }
}
/*void windows::message_window::start(string title,vector <string> content,int s){
    int yp = 200;
    int xp = 300;
    int w = 100;
    int h = 50;
    int a[4];
    int b[4];

    a[0] = utilsf::randrange(0,256,s);
    a[1] = utilsf::randrange(0,256,s);
    a[2] = 200;
    a[3] = 255;

    b[0] = 200;
    b[1] = utilsf::randrange(0,256,s);
    b[2] = utilsf::randrange(0,256,s);
    b[3] = 255;

    bwindow(title,xp,yp,w,h,a,b);
}*/
void windows::message_window::test(){

}
message_window* get_message_window(string title,string msj,int s){
    int a[4];
    int b[4];

    a[0] = utilsf::randrange(0,256,s);
    a[1] = utilsf::randrange(0,256,s);
    a[2] = 200;
    a[3] = 255;

    b[0] = 200;
    b[1] = utilsf::randrange(0,256,s);
    b[2] = utilsf::randrange(0,256,s);
    b[3] = 255;
    message_window *w = new message_window(title,msj,a,b);
    return w;
}

Here window_types.h 在这里window_types.h

/*
 * window_types.h
 *
 *  Created on: Aug 10, 2013
 *      Author: newtonis
 */

#ifndef WINDOW_TYPES_H_
#define WINDOW_TYPES_H_

#include <string>
#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
#include "SDL/SDL_ttf.h"
#include "../../utils/SDL_functions.h"

#include "window.h"
#include "../../extra_data/extra_data.h"

using namespace std;

namespace windows{


    class message_window : public bwindow{
        private:
            vector <string> message;
            string title;
        public:
            message_window(string,string,int a[],int b[]);
            void start(string,vector<string>,int);
            void test();
    };

    message_window* get_message_window(string,string,int);

}

#endif /* WINDOW_TYPES_H_ */

And here I am calling the function get_message_window and I am getting the error "undefined reference to `windows::get_message_window(std::string, std::string, int)" 在这里,我调用了函数get_message_window,并且收到错误消息“未定义对`windows :: get_message_window(std :: string,std :: string,int)的引用”

#include <vector>
#include <string>

#include "SDL/SDL.h"

#include "../utils/event.h"
#include "window/window.h"
#include "window/window_types.h"

#include "stage.h"

#include <iostream>

using namespace std;
using namespace windows;

stage::stage(){
    int a[4];
    a[0] = 100;
    a[1] = 150;
    a[2] = 200;
    a[3] = 255;
    int b[4];
    b[0] = 245;
    b[1] = 218;
    b[2] = 129;
    b[3] = 255;
    add_window( new bwindow("Test window",20,20,200,200,a,b) );

    //ERROR HERE!
    message_window *w = windows::get_message_window("hello","hello",5);


    add_window(w);

}
void stage::graphic_update(SDL_Surface* screen){
    for (int x = 0;x < windws.size();x++){
        windws[x]->graphic_update(screen);
    }
}
void stage::logic_update(events *evs){
    for (int x = 0;x < windws.size();x++){
        windws[x]->logic_update(evs);
    }
}
void stage::add_window(bwindow *win){
    cout<<" Window titled "<<win->get_name()<<" added"<<endl;
    windws.push_back(win);
}

Apparently, the function declaration in the header (which you haven't shown) puts the function into namespeace windows . 显然,标头中的函数声明(未显示)将函数放入namepeace windows But the function definition is in the global namespace. 但是函数定义在全局名称空间中。 In other words, you've implemented one function, named ::get_message_window , but you are calling a different one, named windows::get_message_window . 换句话说,您已经实现了一个名为::get_message_window函数,但是您正在调用另一个名为windows::get_message_window

Either take the declaration out of a namespace, or put the definition into the namespace. 将声明从名称空间中取出,或者将定义放入名称空间中。

According to the error message: 根据错误信息:

message_window* get_message_window(string title,string msj,int s){

should be: 应该:

message_window* windows::get_message_window(string title,string msj,int s){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM