newtonis
newtonis

Reputation: 47

Returning class pointer C++

I have 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
 *
 *  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)"

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

Upvotes: 0

Views: 125

Answers (2)

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

Apparently, the function declaration in the header (which you haven't shown) puts the function into namespeace 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.

Either take the declaration out of a namespace, or put the definition into the namespace.

Upvotes: 3

Borgleader
Borgleader

Reputation: 15916

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){

Upvotes: 2

Related Questions