Thread: Default Constructor Question

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Default Constructor Question

    This question is not a homework question, but a question about an example in the book, Object-Oriented Programming in C++ by Richard Johsonbaugh page 127, Example 3.5.6. What do I need to add to this code segment to make it run:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    unsigned count = 0;
    class C
    {
    public:
    
    C ( ) { cout << "Creating C" << ++count << '\n'; }
    };
    
    {
    C  ar[ 1000 ];

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Um, int main() and a closing brace?

    By the way, it may still not compile. count is the name of a standard algorithm that may be included from your includes. So I guess, with the using directive there may be a naming conflict (I get "count undeclared"), and you should either remove the using directive or indicate that you mean the global variable count: ++::count.

    The Comeau online compiler confirms:
    Code:
    "ComeauTest.c", line 9: error: "count" is ambiguous
      C ( ) { std::cout << "Creating C" << ++count << '\n'; }
                                             ^
    Last edited by anon; 09-27-2008 at 04:44 PM.
    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).

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Thanks anon, but I am confused. The book states:
    "produces the output

    Creating C1
    Creating C2
    ...
    Creating C999
    Creating C1000

    The default constructor is invoked automatically for each of the 1,000 c objects in the array"

    Yet when I added int main ( ) it still did not run???? I'm lost here....help.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have to be more specific.

    Did the program compile?
    If not, what errors did you get?
    Did you see a black window open and close instantly?

    When all errors are fixed, it should indeed produce that output.

    (What puzzles me, how did you get this far in the book if you can't figure out you're missing int main() ???)
    Last edited by anon; 09-27-2008 at 05:32 PM.
    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).

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    I did try using int main( ) before I posted the question. This is the error I am getting along with the complete program:

    Code:
    #include <iostream>
    using namespace std;
    
    
    unsigned count = 0;
    class C
    {
    public:
    
    C ( ) {cout <<  "Creating C" << ++count '\n'; }
    
    };
    
    
    int main() {
    C  ar[ 1000 ];
    
    
    
    return 0;
    }
    "127.cpp" 25 lines, 184 characters
    admiral% g++ 127.cpp
    127.cpp: In constructor `C::C()':
    127.cpp:14: error: use of `count' is ambiguous
    127.cpp:9: error: first declared as `unsigned int count' here
    /usr/local/gcc3/bin/../lib/gcc/sparc-sun-solaris2.8/3.4.5/../../../../include/c++/3.4.5/bits/stl_algo.h:413: error: also declared as `typename std::iterator_traits<_Iterator>::difference_type std::count(_InputIterator, _InputIterator, const _Tp&)' here
    127.cpp:14: error: `count' was not declared in this scope
    127.cpp:14: error: expected `;' before '\xa'

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    anon was right about the ambiguous name.

    Use a different variable name than count. The standard library uses the name count for a function in the std namespace. The compiler can't figure out which version of count to use, yours or the standard library's. If you rename count to something else (like numTimesCalled) then it will work better.

    If you don't want to rename your variable, then you could remove the using namespace std line and refer to cout as std::cout. You could also try changing count to ::count as anon suggested, or change using namespace std to using std::cout. These are all solutions to get around the fact that you use a variable named count and there is a function in the std namespace called count as well.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Smile

    Thanks Daved. You and anon helped me understand the example much better than the book!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Default Constructors w/Classes as Parameters
    By GrNxxDaY in forum C++ Programming
    Replies: 22
    Last Post: 07-31-2002, 07:50 AM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM