0

i create game with opengl (c++) and i have namespace in file .h :

namespace SR
{
    #include "Geometric.h"
    //#include others in this namespace 
}

when i create in main.cpp some object and i call method from the object (Geometric) -> it doesnt work ,but when i erase the namespace in .h file :

//namespace SR
    //{
        #include "Geometric.h"
        //#include others in this namespace 
    //}

it work , why ?.

This is when i erase namespace from .h and call method in .cpp [its work]:

    #include "shooting_range.h"

int main(int agrc,char** agrv)
{
    Geometric p;
    p.getCoordinates();
    return 0;
}

This is when i use namespace (error : undefined reference to SR::Geometric::Geometric())

    #include "shooting_range.h"

int main(int agrc,char** agrv)
{
    SR::Geometric p;
    p.getCoordinates();
    return 0;
}

And there i try add brackets [error : error: request for member ‘getCoordinates’ in ‘p’, which is of non-class type ‘SR::Geometric()]

#include "shooting_range.h"

int main(int agrc,char** agrv)
{
    SR::Geometric p();
    p.getCoordinates();
    return 0;
}

I don't know why it doesn't work :-( My english is bad i am sorry ... I try to explain the best ... I am confused .

_________- edit : I relocate namespace to geometric.h , but still doesnt work . (request for member ‘getCoordinates’ in ‘p’, which is of non-class type ‘SR::Geometric()) Geometric.h :

#ifndef _GEOMETIC_H_
#define _GEOMETIC_H_

#include <GL/gl.h>

#define MAX_VERTEX 300

struct coordinates
{
    GLdouble x,y,z;
    GLdouble n_x,n_y,n_z;
    GLubyte r,g,b;
};

namespace SR
{


class Geometric
{
    public :
        int a;
        Geometric();

        void draw();

        void rotate(GLdouble angle,GLdouble x,GLdouble y,GLdouble z);
        void translate(GLdouble x,GLdouble y,GLdouble z);
        void scale(GLdouble x,GLdouble y,GLdouble z);

        coordinates* getCoordinates(); //get all vertex
        coordinates getCoordinates(GLint); //get data spacific vertex - if vertex == null => error

        GLboolean setCoordinates(coordinates*,GLint); //initialization all vertex through parameters
        GLboolean setCoordinates(coordinates,GLint); //change data specific vertex


    protected :
        GLshort number; //number of vertex, maximal number of vertex is macro MAX_VERTEX
        coordinates* vertex;
        GLdouble matrix[16];
};

}

#endif // _GEOMETIC_H_

Geometric.cpp

#include "Geometric.h"

SR::Geometric::Geometric()
{
    vertex = NULL;
    number = 0;
}

coordinates* SR::Geometric::getCoordinates()
{
    return vertex;
}

coordinates SR::Geometric::getCoordinates(GLint i)
{
    return vertex[i];
}

GLboolean SR::Geometric::setCoordinates(coordinates* vertex,GLint number)
{
    if(number <= 0 || number > MAX_VERTEX)
        return -1;

    if(this->vertex)
        delete[] vertex;

    this->vertex = new coordinates[number];
    this->number = number;

    for(int i = 0;i < number;i++)
    {
        this->vertex[i] = vertex[i];
    }
        return 0;
}

GLboolean SR::Geometric::setCoordinates(coordinates vertex,GLint i)
{
    if(number <= 0 || i >= this->number)
        return -1;

    this->vertex[i] = vertex;

        return 0;
}

#include "shooting_range.h"

main.cpp

int main(int agrc,char** agrv)
{
    SR::Geometric p();
    p.getCoordinates();
    return 0;
}
S3jp4kCZE
  • 15
  • 6
  • Why do you `#include` in the namespace instead of defining your classes in the namespace directly? – Some programmer dude Jun 15 '18 at 06:15
  • 3
    `Geometric` should be declared and defined in the same namespace. By putting `#include` inside of namespace all the declarations in that include file will move inside of the surrounding namespace. While definitions (which are most likely in cpp file) remain in the global namespace as before. – user7860670 Jun 15 '18 at 06:20
  • 1
    As for *one* your problem, the hint is in the error message: "... which is of **non-class type ‘SR::Geometric()**". That is, `p` is not an object, it's a *function* which takes no arguments and returns a `SR::Geometric` object. – Some programmer dude Jun 15 '18 at 06:20
  • because i want more classes with namespace ::SR ,and then i just all files .h include to the namespace . – S3jp4kCZE Jun 15 '18 at 06:22
  • 1
    In this case correct approach would be to use type alias: `#include "Geometric.h namespace SR{ using Geometric = ::Geometric; }` This way `Geometric` will still remain in global namespace, but can be used like `SR::Geometric p;` as well – user7860670 Jun 15 '18 at 06:23
  • Then just define those classes inside the namespace as well, in their own header files. That will make it much easier to include the header files *separately* in case you don't need *all* of them (makes compilation quicker). – Some programmer dude Jun 15 '18 at 06:24
  • Its work when i have namespace in geometric.h and i delete brackets THNAKS !, but i still dont know why i cant do this : namespace SR { #include "Geometric.h" } – S3jp4kCZE Jun 15 '18 at 06:54
  • this should never be done? explain it to me semioptically [bad english :(] – S3jp4kCZE Jun 15 '18 at 06:55
  • but my namespace in geometric.h work :-) so nice guys – S3jp4kCZE Jun 15 '18 at 07:36
  • good , now I understand the problem ! – S3jp4kCZE Jun 15 '18 at 08:02

0 Answers0