Won't see as function declaration

This is a discussion on Won't see as function declaration within the C++ Programming forums, part of the General Programming Boards category; I'm getting some mysterious syntax errors. Anywhere I declare grid::region& as the return type of a function, I get trouble. ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891

    Won't see as function declaration

    I'm getting some mysterious syntax errors. Anywhere I declare grid::region& as the return type of a function, I get trouble. I expect it's some little typo mistake, but I can't find it after combing a while.
    Code:
    simulator.h:25: error: ‘region’ declared as a ‘virtual’ field
    simulator.h:25: error: expected ‘;’ before ‘(’ token
    simulator.h:48: error: expected ‘;’ before ‘(’ token
    simulator.h:53: error: expected `;' before ‘private’
    simulator.h:72: error: expected ‘;’ before ‘(’ token
    simulator.h:73: error: expected `;' before ‘}’ token
    Code:
    class thread_method
    {
      public:
        virtual bool wait() = 0;
        virtual grid::data_type get_sum(const grid::data_type & piece) = 0;
        virtual grid::data_type get_max(const grid::data_type & piece) = 0;
        virtual grid::region & region(std::vector<grid::region>::size_t index) = 0;
    };
    
    class single_thread : public thread_method
    {
        public:
            bool wait() { return true; }
            grid::data_type get_sum(const grid::data_type & piece) { return piece; }
            grid::data_type get_max(const grid::data_type & piece) { return piece; }
            grid::region & region(std::vector<grid::region>::size_t index)
            {
                if(index) throw std::string("single_thread requested region index other than 0");
                return r;
            }
        private:
            grid::region r;
    };
    Code:
    class grid
    {
       .  .  .
          typedef std::vector<span> region;
       .  .  .
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Is the grid class visible? Have you made sure you have included the proper header?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Yes, it is visible.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And public?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    And you say it only happens when the return type is grid::region and not any of the parameters?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Correct. Here's grid, for good measure:
    Code:
    class grid : public array2d<GRID_DATA_TYPE, uword>
    {
    public:
        typedef array2d<grid::data_type,uword> base_type;
    	typedef array2d<grid::data_type,uword>* base_pointer_type;
    	grid(size_type w_in, size_type h_in, data_type fill = 0.0); //see declaration of array2d for 'data_type'
    
    	struct span;  //POD type
    	typedef std::vector<span> region;
    	static size_type num_points(const region & r, size_type until = std::numeric_limits<size_type>::max());
    
    	bool iterate_region(const region & reg, func2d & f);
    	void write_file(const std::string & path);
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you have to define span before you can typedef it? IIRC, typedefs don't change when you redefine names, so right now region is a vector of an incomplete type.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    However, I think it's time for "minimal, complete example that shows the problem". Reduce it down to one file, check that you still get the problem, and post it here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Here's a gentle program that completely rapes my compiler. How is span an incomplete type?
    Code:
    #include <vector>
    #include <iostream>
    
    class grid
    {
      public:
        struct span
        {
            int x; int y; int dy;
        };
        typedef std::vector<span> region;
    };
    
    class thread_method
    {
      public:
        grid::region & region(std::vector<grid::region>::size_t index) = 0;
    };
    
    class single_thread : public thread_method
    {
        grid::region r;
      public:
        grid::region & region(std::vector<grid::region>::size_t index) { return r; }
    };
    
    int main()
    {
        thread_method * tmeth = new single_thread();
        std::cout << tmeth->region(0).size() << std::endl;
        delete tmeth;
    }
    Code:
    david@david-laptop:~/Desktop$ g++ prob.cpp -o prob
    prob.cpp:19: error: expected ‘;’ before ‘(’ token
    prob.cpp:26: error: expected ‘;’ before ‘(’ token
    prob.cpp:27: error: expected `;' before ‘}’ token
    prob.cpp: In function ‘int main()’:
    prob.cpp:32: error: ‘class thread_method’ has no member named ‘region’
    /usr/include/c++/4.2/bits/stl_vector.h: In destructor ‘std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = grid::span, _Alloc = std::allocator<grid::span>]’:
    /usr/include/c++/4.2/bits/stl_vector.h:199:   instantiated from ‘std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = grid::span, _Alloc = std::allocator<grid::span>]’
    prob.cpp:23:   instantiated from here
    /usr/include/c++/4.2/bits/stl_vector.h:120: error: invalid use of incomplete type ‘struct grid::span’
    prob.cpp:7: error: forward declaration of ‘struct grid::span’
    /usr/include/c++/4.2/bits/stl_construct.h: In function ‘void std::__destroy_aux(_ForwardIterator, _ForwardIterator, std::__false_type) [with _ForwardIterator = grid::span*]’:
    /usr/include/c++/4.2/bits/stl_construct.h:155:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = grid::span*]’
    /usr/include/c++/4.2/bits/stl_construct.h:182:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>) [with _ForwardIterator = grid::span*, _Tp = grid::span]’
    /usr/include/c++/4.2/bits/stl_vector.h:268:   instantiated from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = grid::span, _Alloc = std::allocator<grid::span>]’
    prob.cpp:23:   instantiated from here
    /usr/include/c++/4.2/bits/stl_construct.h:121: error: cannot increment a pointer to incomplete type ‘grid::span’
    /usr/include/c++/4.2/bits/stl_construct.h: In function ‘void std::_Destroy(_Tp*) [with _Tp = grid::span]’:
    /usr/include/c++/4.2/bits/stl_construct.h:122:   instantiated from ‘void std::__destroy_aux(_ForwardIterator, _ForwardIterator, std::__false_type) [with _ForwardIterator = grid::span*]’
    /usr/include/c++/4.2/bits/stl_construct.h:155:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator) [with _ForwardIterator = grid::span*]’
    /usr/include/c++/4.2/bits/stl_construct.h:182:   instantiated from ‘void std::_Destroy(_ForwardIterator, _ForwardIterator, std::allocator<_T2>) [with _ForwardIterator = grid::span*, _Tp = grid::span]’
    /usr/include/c++/4.2/bits/stl_vector.h:268:   instantiated from ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = grid::span, _Alloc = std::allocator<grid::span>]’
    prob.cpp:23:   instantiated from here
    /usr/include/c++/4.2/bits/stl_construct.h:107: error: invalid use of incomplete type ‘struct grid::span’
    prob.cpp:7: error: forward declaration of ‘struct grid::span’
    david@david-laptop:~/Desktop$
    Last edited by CodeMonkey; 01-19-2009 at 01:44 PM. Reason: forgot to delete
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is not with span or region. Vector does not have a nested typedef called size_t, it's size_type.

    Comeau online told me that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Quote Originally Posted by CodeMonkey View Post
    I expect it's some little typo mistake, but I can't find it after combing a while.
    It was.
    Code:
    class thread_method
    {
      public:
        virtual grid::region & region(std::vector<grid::region>::size_type index) = 0;
    };
    
    class single_thread : public thread_method
    {
        public:
            grid::region & region(std::vector<grid::region>::size_type index)
            {
                if(index) throw std::string("single_thread requested region index other than 0");
                return r;
            }
        private:
            grid::region r;
    };

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    891
    Yes, that fixed it. Also, I had been saving to prob.txt and compiling prob.cpp..... it's no wonder none of the proposed solutions were working!

    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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