c++ - 返回类指针 C++

标签 c++ class function pointers return

我有 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;
}

这里是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_ */

我在这里调用函数 get_message_window 并收到错误“对 `windows::get_message_window(std::string, std::string, int) 的 undefined reference ”

#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);
}

最佳答案

显然, header 中的函数声明(您未显示)将函数放入命名空间windows。但是函数定义在全局命名空间中。换句话说,您已经实现了一个名为 ::get_message_window 的函数,但您正在调用另一个名为 windows::get_message_window 的函数。

要么从命名空间中取出声明,要么将定义放入命名空间。

关于c++ - 返回类指针 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167741/

相关文章:

c++ - 具有冲突名称的类的构造函数

c++ - 命名空间中定义的类中的友元函数

java - 搜索特定的类并返回它

sql - 错误 : return type mismatch in function declared to return

Javascript 函数作为对象属性返回

Javascript 异步函数控制台记录返回的数据

c++ - 循环失败

c++ - PThread 初学者 - 启动、同步、停止工作线程

c++ - TypeCasting 在 C++ 中将 <int, class A> 映射到 <int, class B>

C++ 返回对临时对象的引用或将它们存储在对象中