Thread: object not creating????

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Angry object not creating????

    Hi,
    here is my program with the following error when I run it.
    I want to make program that can calculate no. of objects but objects are not creating here. whats is the problem and plz resolve it thanks.


    Error: ...source.cpp(7): error C2872: 'count' : ambiguous symbol


    Code:
    ////count.h//////
    
    #pragma once
    class count
    {
    public:
        count(void);
        static int counter;
        void print();
        ~count(void);
    };

    Code:
    ////count.cpp////
    #include "count.h"
    #include<iostream>
    using namespace std;
    
    count::count(void)
    {
        counter++;
    }
    
    void count::print()
    {
    cout<<"No. of Objects: "<< counter;
    
    }
    
    count::~count(void)
    {
    }


    Code:
    ////source.cpp////
    
    
    #include<iostream>
    #include"count.h"
    using namespace std;
    void main()
    {
    
        count o1, o2, o3;
    
        o1.print();
    
        system("pause");
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It interferes with std::count. Create your own namespace (e.g.: namespace MyProgram ) and put there all your classes/functions. And avoid using namespace std, especially in headers.

    Also:
    - #pragma once is nonportable, use header guards instead.
    - use int main(), not void main()
    - In C++, parameterless functions don't need to be marked (void).
    - avoid std::system()

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's because you've run afoul of namespace collisions. There is a function named count in the std namespace, as well.
    So either you have to explicitly qualify which you meant by "::count", or you have to get rid of using namespace std.
    Btw, main shall return int and nothing else.
    system is dangerous. I suggest you not use it. Here are alternatives: SourceForge.net: Pause console - cpwiki
    And there is no need to type "void" in an empty parameter list in C++. Just keep it as "()" instead.
    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.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Please don't send out messages to strangers asking for help (especially after you have already got some excellent answers).
    All 'active' members, by definition, visit the boards they are interested in regularly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating moving object. is any way to do it in other way?
    By Grouw Patter in forum C++ Programming
    Replies: 12
    Last Post: 10-27-2012, 10:12 AM
  2. Creating an object at run-time
    By Mavix in forum C# Programming
    Replies: 7
    Last Post: 06-13-2007, 05:46 AM
  3. creating object with [new] ?
    By wolfindark in forum C++ Programming
    Replies: 11
    Last Post: 05-13-2007, 12:31 PM
  4. Replies: 4
    Last Post: 11-14-2006, 11:52 AM
  5. Creating object arrays for VC++
    By tineras in forum C++ Programming
    Replies: 23
    Last Post: 05-07-2006, 02:39 AM