Problem with structure and class

This is a discussion on Problem with structure and class within the C++ Programming forums, part of the General Programming Boards category; Hi ! Can I declare structure inside class and return pointer to structure......... like this....... //xml.h struct line { int ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    104

    Problem with structure and class

    Hi !

    Can I declare structure inside class and return pointer to structure.........

    like this.......
    //xml.h

    struct line
    {
    int width;
    int color;
    int length;
    };


    class XML
    {

    public:
    line* read(char* filepath);
    int m_width,m_color,m_length;
    };


    [/code]

    Implementation of XML file.........

    Code:
    line* Xml::read(char* filepath)
    {
       line *m_line;
    //some operation of parsing file
    
      m_line->width=m_width;
      m_line->color=m_color;
      m_line->length=m_length;
    
    return line;
    }

    And this function ic called from fileParser.cpp

    Code:
    Xml *xml=new Xml();  
    
    line *m_line;
    
    m_line=xml->read(filepath);
    cout<<m_line->width<<line_color<<line_length;
    }

    It is compiling but giving segmentation fault.

    Can any body tell me that where is problem........or should I change implementation..

    Thanks

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You need to allocate memory for your line. At the moment m_line just points to random memory that isn't owned by your program. When your program tries to use that memory, the OS complains that you haven't asked for that memory and thus shouldn't be allowed to use it and issues a Segmentation Fault signal, which terminates your program.
    Use new to allocate some memory first:
    Code:
      line *m_line = new line;
    //some operation of parsing file
    
      m_line->width=m_width
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    you did allocate memory for m_line with new, right?

    m_line = new line;

    also your quote shows 'return line', when perhaps it should be 'return m_line'.

    but really, you have not pasted enough code or other information to tell why you are getting a seg fault.

    unrelated to your programming problems, XML is a very complex format. if you can avoid it, i recommend finding a 3rd party library to parse it for you. or, if you have a choice in the matter, switching to a more lightweight & friendly format, such as s-expressions:

    http://en.wikipedia.org/wiki/S-expression
    http://people.csail.mit.edu/rivest/sexp.html

    the 2nd hosts the source code for a c s-expression parser which i have not used before, but the truth is s-expressions are much better suited to writing your own parser from scratch than XML, which is quite complex and difficult to do properly.
    .sect signature

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    104
    Thanks QuantumPete and ggs for your help...........

    Bargi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure vs. class
    By $l4xklynx in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 03:00 AM
  2. Abstract class (IDispatch) in a C structure
    By Overlord in forum Windows Programming
    Replies: 4
    Last Post: 12-31-2008, 08:38 AM
  3. Class Membership Problem
    By josephjah in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 02:48 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21