-2

I have this class

class Field
{
    public:
        Field();
        ~Field();
        Field(const std::string& nameP, 
              const int idP , 
              const std::map<int,Field*> sequence=std::map<int,Field*>(), 
              const std::string& constval="",
              const std::string& presP="");
        std::string f_name;
        int f_id;
        std::string f_presence;
        std::string f_const;
        std::map<int,Field*> f_set;
};

the problem is that my Class Field can be a set of other fields that's why I've used the map , or it can be a constant value that's why I used the const, or it could be neither so I used 3 different constructor , but the compiler refuses them telling me "C++ Ambiguous call to overloaded Field... Candidates are ...) so now I tried to put all possibilities in one but the compiler once again giving me loads of errors since my last three fields. So anyone can tell me what should I do to have my three possible constructors ?? PS: I really need to know the answer about this ambiguity call so please no inheritance suggestion please ! ps the previous code causing the error is this

class Field
{
    public:
        Field();
        ~Field();
        Field(const std::string& typeP, 
              const std::string& nameP, 
              const int idP , 
              const std::string& presP="");
        Field(const std::string& typeP, 
              const std::string& nameP, 
              const int idP , 
              const std::string& constval, 
              const std::string& presP="");
        Field(const std::string& typeP, 
              const std::string& nameP, 
              const int idP , 
              const std::map<int,Field*> sequence , 
              const std::string& presP="");
    /// field attributes
        std::string f_type;
        std::string f_name;
        int f_id;
        std::string f_presence;
        std::string f_const;
        std::map<int,Field*> f_set;
};
Joy
  • 1,707
  • 7
  • 29
  • 44
  • 2
    Show your code that actually causes the compiler error. – ildjarn May 11 '12 at 17:41
  • @CodyGray - the ambiguity is in the the previous version of his code, the version he *hasn't posted*. – Robᵩ May 11 '12 at 17:45
  • 2
    Glolita, please reduce your original program to a small, *complete* sample program. (small=20 lines or so, complete=we can copy-paste your code into a file and compile it). Make sure that the small, complete program still demonstrates the error you are having. Then copy-paste that program into your question. For more info see http://sscce.org/. – Robᵩ May 11 '12 at 17:47
  • in my main function I only did FASTField* ff=new FASTField(typeP, nameP, idP ,presP); and the ambiguity get generated ! – Joy May 12 '12 at 08:43

1 Answers1

0

This might be a GCC bug(57, 39426); it works in GCC 4.6, fails in 4.4. Try wrapping the default value in parenthesis:

#include <map>
#include <string>


class Field
{
    public:

        Field();
        ~Field();
        Field(const std::string& nameP,
              const int idP ,
              const std::map<int,Field*> sequence
                =   ( std::map<int,Field*>() ), //<<<---- Fix is to () the default value
              const std::string& constval="",
              const std::string& presP="");
        std::string f_name;
        int f_id;
        std::string f_presence;
        std::string f_const;
        std::map<int,Field*> f_set;
};
int main() {}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308