Thread: constructors

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    constructors

    Hi there! i am facing a problem when i call the overloaded constructor from the main function. For example:

    Code:
    
    
    Code:
    const char* namesList[5]= {"Brad Shaw","Aimen Adams","Sal Dimitry","CristiAnreaz","Poala James"};
    
    int idList[]={232,444,135,52,134};



    Code:
    
    
    Code:
    void main()
    {
       Team t(namesList,idList,5,"Team Blasters");
    }



    my constructor is:

    Code:
    
    
    Code:
    Team::Team(char sNames_List[], int id_List[], int No_Of_Players, char* Name)
    {
        
    
    
        for (int i=0; i<No_Of_Players; i++)
        {
            players[i].Set_Id(id_List[i]);
            players[i].Set_Name(Name);
            cout << players[i].Get_Name() << endl;
        }
    
    
    }



    it underlines the namesList in the main and says that no instance of constructor Team::Team matches the argument list

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the source file that defines main(), you need to ensure the compiler has visibility of the definition of class Team. For example, if the class is defined in team.h, there needs to be #include <team.h> before the definition of main().

    You also need to ensure the three pointer arguments of Team's constructor are declared const. const arrays (which cannot be changed) cannot be passed as a non-const reference/pointer/array argument (which would permit changing them).

    Also, in C++, main() returns int, not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Your constructor expects an array of characters while but you provide it with an array of strings instead
    Code:
    char * foo ;//array of characters 
    char foo[];//array of characters
    char * foo[] ;//array of strings
    Last edited by Aslaville; 07-15-2013 at 05:34 AM.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by Aslaville View Post
    Your constructor expects an array of characters while but you provide it with an array of strings instead
    Code:
    char * foo ;//array of characters 
    char foo[];//array of characters
    char * foo[] ;//array of strings

    what should i do to the constructor in the main function?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    actually it uses C-strings. And since this is C++ the std::string would be better IMHO
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    class Test
    {
        std::vector<std::string> _vs;
    public: 
        Test(const std::vector<std::string> &vs):_vs(vs)
        {
        }
        void print()
        {
            for(size_t i=0;i<_vs.size();i++)
            {
                std::cout << _vs[i] << std::endl;
            }
        }
    };
    int main(void)
    {
        std::string init[] = {"test1","test2","test3"};
        size_t count = sizeof(init)/sizeof(init[0]);
        std::vector<std::string> v(init, init+count);
        Test t =Test(v);
        t.print();
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by waqas94 View Post
    what should i do to the constructor in the main function?
    Use std::string, it will save you a lot of headache

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Am I guaranteed this will give me the number of strings in the array ?

    Code:
    size_t count = sizeof(init)/sizeof(init[0])

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it will. Though you should remember that if you a container (eg std::array, std::vector), it has a .size() function that returns the size.
    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.

  9. #9
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Elysia View Post
    Yes, it will. Though you should remember that if you a container (eg std::array, std::vector), it has a .size() function that returns the size.
    I bet it looks ugly

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Aslaville View Post
    Am I guaranteed this will give me the number of strings in the array ?

    Code:
    size_t count = sizeof(init)/sizeof(init[0])
    Actually, no. Success of this construct depends on context.

    If init is a pointer (which includes arrays that are arguments of functions) then the above is not guaranteed to work as you expect.

    If init is actually an array, then that construct gives the number of elements in that array.

    For example;
    Code:
    #include <iostream>
    
    void f(char *p)
    {
         std::cout << "In f() " << sizeof(p)/sizeof(p[0]) << '\n';
    }
    
    int main()
    {
         char p[5];
         std::cout << "In main() " << sizeof(p)/sizeof(p[0]) << '\n';
         f(p);
         return 0;
    }
    You will find that this code will print different values in main() and in f().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by grumpy View Post
    Actually, no. Success of this construct depends on context.

    If init is a pointer (which includes arrays that are arguments of functions) then the above is not guaranteed to work as you expect.

    If init is actually an array, then that construct gives the number of elements in that array.

    For example;
    Code:
    #include <iostream>
    
    void f(char *p)
    {
         std::cout << "In f() " << sizeof(p)/sizeof(p[0]) << '\n';
    }
    
    int main()
    {
         char p[5];
         std::cout << "In main() " << sizeof(p)/sizeof(p[0]) << '\n';
         f(p);
         return 0;
    }
    You will find that this code will print different values in main() and in f().
    Oooh that is nice.Thanks for taking a look.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is actually a trick, if you are willing to endure some template magic:

    Code:
    template<size_t N, typename T>
    size_t ArraySize(const T(&)[N])
    {
    	return N;
    }
    
    void foo(int arr[])
    {
    	std::cout << ArraySize(arr) << "\n"; // Fails to compile
    }
    
    int main()
    {
    	int arr[10];
    	std::cout << ArraySize(arr) << "\n"; // Compiles happily: returns 10
    }
    This protects you from passing a pointer, if you need it.
    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.

  13. #13
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    What trick is this,...I mean where can I read about it from a book ?
    Code:
    size_t array_size ( const  T (&) [N] )
    Am working through templates and haven`t come across it yet.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't think you will this in a book.
    const T(&Name)[N] is type for passing an array by const reference where Name is the name of the argument.
    T is the type and N is the size. I substituted those for template parameters so they could be anything.
    An array of any type and an array of any size.
    Then I just return the size.
    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.

  15. #15
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Elysia View Post
    I don't think you will this in a book.
    const T(&Name)[N] is type for passing an array by const reference where Name is the name of the argument.
    T is the type and N is the size. I substituted those for template parameters so they could be anything.
    An array of any type and an array of any size.
    Then I just return the size.
    Okay I got it.I got confused by the syntax.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2008, 01:21 AM
  2. Help with constructors!
    By -Syntax in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2008, 10:23 PM
  3. constructors?
    By sayword in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 11:56 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. Constructors
    By wireless in forum C++ Programming
    Replies: 6
    Last Post: 03-03-2002, 11:25 AM