Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with namespace : undefined reference.  (Read 2561 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Problem with namespace : undefined reference.
« on: February 28, 2014, 11:26:02 am »
Hi, I've a namespace odfaeg and sub namespaces g2d and g3d :

But when I want to use the classes of the namespace odfaeg::g3d which have the same name than those one contained in the namespace odfaeg::g2d, the compialtor gives me undefined reference errors.
 
int main() {

    odfaeg::g3d::RenderWindow window(sf::VideoMode(800, 600), "TestODFAEG");
    sf::Texture *text;
    text->loadFromFile("tilesets/herbe.png");
    odfaeg::g3d::Tile *tile = new odfaeg::g3d::Tile(text, odfaeg::Vec2f(0, 0), odfaeg::Vec2f(100, 50), sf::IntRect(0, 0, 100, 50),0);

   // BoundingRectangle rect (0, 0, 1000, 1000);
    //generate_floor(tiles,rect);
    while(window.isOpen()) {
        window.clear(sf::Color(0, 0, 0));
        window.display();
        sf::Event event;
        while(window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }
}
 

Tile inherit from entity and entity inherits from transformable and drawable, and I've putting all the source code of all those classe in this namespaces :

namespace odfaeg {
     namespace g3d {

}
}
 
For the 3D classes and
namespace odfaeg {
     namespace g2d {
     }
}
 
For the 2D classes.
To avoid to have to prefix the classes like this : G2DEntity and G3DEntity.


But it seems it doesn't work and it always look in the g2d namespace even if I define the subnamespace without using the using keywork.

int main() {

    odfaeg::g3d::RenderWindow window(sf::VideoMode(800, 600), "TestODFAEG");
    sf::Texture *text;
    text->loadFromFile("tilesets/herbe.png");
    odfaeg::g3d::Tile *tile = new odfaeg::g3d::Tile(text, odfaeg::Vec2f(0, 0), odfaeg::Vec2f(100, 50), sf::IntRect(0, 0, 100, 50),0);

   // BoundingRectangle rect (0, 0, 1000, 1000);
    //generate_floor(tiles,rect);
    while(window.isOpen()) {
        window.clear(sf::Color(0, 0, 0));
        window.display();
        sf::Event event;
        while(window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }
}
 

Code: [Select]
D:\Projets\Projets-c++\Test3DODFAEG\main.cpp|34|undefined reference to `odfaeg::g3d::Tile::Tile(sf::Texture const*, odfaeg::Vec2f, odfaeg::Vec2f, sf::Rect<int>, int, odfaeg::g3d::Entity*)'|

I don't know why but the compilator go in the namespace odfaeg::g2d and not in the namespace odfaeg::g3d.

I think I'll get rid of the subnamespace g2d by putting all the 2d classes in the namespace odfaeg and let the 3d classes in the namespace odfaeg::g3d....

But it really suck. :/





« Last Edit: February 28, 2014, 11:34:30 am by Lolilolight »

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Problem with namespace : undefined reference.
« Reply #1 on: February 28, 2014, 11:44:17 am »
Are you sure you gave the good parameters ?

Because I can't understand how it's possible to give a 3D position with a 2D Vector (unless you cast Vector2 in Vector3), define a size of a 3D element with a 2D Vector, I don't see where you define the mesh the texture will be applied to, can't see what the IntRect is used for.

Do you have a "using namespace something" somewhere ?

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Problem with namespace : undefined reference.
« Reply #2 on: February 28, 2014, 01:14:06 pm »
This is a 2D position and a 2D size but I want to use 3D vertices to perform a depth test with a fragment shader.

Tile::Tile (const sf::Texture *image, Vec2f position, Vec2f size, sf::IntRect subRect, int zOrder, Entity *parent)
: Entity (Vec3f(position.x, position.y, zOrder), Vec3f(size.x, size.y, 0), Vec3f(size.x  * 0.5f, size.y * 0.5f, 0), "E_TILE") {
    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(4);
    Vertex *quad = &m_vertices[0];
    quad[0].position = sf::Vector3f(position.x, position.y, zOrder);
    quad[1].position = sf::Vector3f(position.x + size.x, position.y, zOrder);
    quad[2].position = sf::Vector3f(position.x + size.x, position.y + size.y, zOrder);
    quad[3].position = sf::Vector3f(position.x, position.y + size.y, zOrder);
    quad[0].texCoords = sf::Vector2f(subRect.left, subRect.top);
    quad[1].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top);
    quad[2].texCoords = sf::Vector2f(subRect.left + subRect.width, subRect.top + subRect.height);
    quad[3].texCoords = sf::Vector2f(subRect.left, subRect.top + subRect.height);
    addTexture(subRect, image);    
}
 

Code: [Select]
uniform sampler2D texture;
void main () {

    // récupère le pixel dans la texture
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
if (pixelColor.a >= gl_FrontColor.a) {
gl_FragDepth = gl_FragCoord.z;
if (glDepthFunc(GL_GREATER))
gl_FragColor = gl_Color * pixel;
} else {
gl_FragColor = gl_Color * pixel;
}
}

Because a tilemap don't allow me to have entities with different z position.

And if I enable the defaut opengl dephtest it doesn't work for semi-transparent textures.

That's a real problem.

Here I don't want to do the depth test if the current pixel color is more transparent than the pixel color of the screen.

This si why I want to put the 2D positions and sizes into 3D vectors.

PS : I've put the 2D classes into the odfaeg namespace and deleted the subnamespace g2d  but it does'nt solve the problem. :/

It seems that the compiler is still considering that there are in the same namespace.

« Last Edit: February 28, 2014, 01:23:46 pm by Lolilolight »

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Problem with namespace : undefined reference.
« Reply #3 on: February 28, 2014, 01:33:35 pm »
Do you have a "using namespace something" somewhere ?

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Problem with namespace : undefined reference.
« Reply #4 on: February 28, 2014, 01:49:47 pm »
No I didn't have a using namespace anywhere except for the common namespaces std and sf in the c++ files.

Why ?

It seems the problem comes with the vtable with doesn't do the difference between the odfaeg::Entity and odfaeg::g3d::Entity when he call the constructor here :
Tile::Tile (const sf::Texture *image, Vec2f position, Vec2f size, sf::IntRect subRect, int zOrder, Entity *parent)
: g3d::Entity (Vec3f(position.x, position.y, zOrder), Vec3f(size.x, size.y, 0), Vec3f(size.x  * 0.5f, size.y * 0.5f, 0), "E_TILE")
 

Because if I put the implementation in the .h file it gives me an undefined reference to the constructor of the class Entity in the vtable.
« Last Edit: February 28, 2014, 01:55:03 pm by Lolilolight »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem with namespace : undefined reference.
« Reply #5 on: February 28, 2014, 01:57:15 pm »
"Undefined reference" means that the linker can't find the definition of the function. Which means that:
- you forgot to link the corresponding library, if the function is imported
- you forgot to define it (it is only declared)
- you forgot to add the .cpp that defines it to your project/makefile

Check these 3 points and you should find the source of the problem.
Laurent Gomila - SFML developer

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Problem with namespace : undefined reference.
« Reply #6 on: February 28, 2014, 01:59:20 pm »
There's a link problem somewhere, the function is not declared or not in the good namespace somewhere.

Edit: arf, Laurent is faster (and more explicit/complete)

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Problem with namespace : undefined reference.
« Reply #7 on: February 28, 2014, 02:08:50 pm »
Ha yes I've forgotten to add them in the CMakeList file, thx my problem is solved. :)