Thread: error: expected constructor, destructor, or type conversion...

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    12

    error: expected constructor, destructor, or type conversion...

    I have some C++ code I am working on that receives the following compilation error:

    Code:
    game.cpp:29:11: error: expected constructor, destructor, or type conversion before ‘(’ token
     Game::Game(date, true, boxCount)
    I cannot seem to find the reason this is occurring, because this is how a constructor is supposed to be. Any advice would be greatly appreciated.


    box.h

    Code:
    #ifndef BOX_H
    #define BOX_H
    #include <string>
    
    class Box
    {
        friend std::ostream& operator<<(std::ostream& o, const Box& b);
        friend bool operator<(const int i, const Box& b);
        friend bool operator==(const int i, const Box& b);
        friend int operator+(const int i, const Box& b);
        public:
            Box(std::string d, bool f, int bc);
            bool operator<(const Box& b) const;
        protected:
            std::string date;
            int productCount;
            int boxCount;
            bool isFull;

    box.cpp

    Code:
    #include <iostream>
    #include <string>
    #include "box.h"
    using namespace std;
    
    Box::Box(string d, bool f, int bc)
        :date(d)
        ,boxCount(bc)
        ,productCount()
        ,isFull(f)
    {
    
    }
    
    ostream& operator<<(ostream& o, const Box& b)
    {
        return o << "Box[" << b.productCount << "]";
    }
    bool operator<(const int i, const Box& b)
    {
        return i < b.productCount;
    }
    
    bool operator==(const int i, const Box& b)  {
        return i == b.productCount;
    }
    
    int operator+(const int i, const Box& b)    {
        return i + b.productCount;
    }
    
    bool Box::operator<(const Box& b) const
    {
        return productCount < b.productCount;
    }

    game.h

    Code:
    #ifndef GAME_H
    #define GAME_H
    #include <string>
    #include "box.h"
    
    class Game : public Box
    {
        friend std::ostream& operator<<(std::ostream& o, const Game& b);
        friend bool operator<(const int i, const Game& b);
        friend bool operator==(const int i, const Game& b);
        friend int operator+(const int i, const Game& b);
        public:
            Game(std::string, bool, int);
    };
    
    #endif

    game.cpp

    Code:
    #include <iostream>
    #include <string>
    #include "game.h"
    #include "box.h"
    using namespace std;
    
    Game::Game(date, true, boxCount)
        :Box(date, true, boxCount)
        ,productCount(6)
    {
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot that the parameter list in a function definition specifies the parameter types too. Also, instead of the value true, you would need to provide the parameter name, e.g.,
    Code:
    Game::Game(std::string date, bool f, int boxCount)
        :Box(date, f, boxCount)
        ,productCount(6)
    {
    
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think it makes sense to compare a Box to an integer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-04-2015, 12:39 PM
  2. Replies: 3
    Last Post: 06-05-2012, 01:14 PM
  3. Replies: 1
    Last Post: 04-15-2011, 02:47 PM
  4. Expected constructor, destructor, type conversion
    By SterlingM in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2010, 01:16 PM
  5. expected constructor, destructor, or type conversion before '('
    By cosmiccomputing in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 11:03 PM

Tags for this Thread