Thread: Class errors

  1. #1
    Unregdperson
    Guest

    Class errors

    can someone tell me why VC++ .net won't compile this, but Dev C++ 4 does ?

    Code:
    #include <iostream>
    
    class clearscreen
    {
     public:void clear()
    {
    
      using namespace std;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
     cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
    }
    
    };
    here is the error from .NET

    c:\cpp\Game\RPG Game\Clearscreen.cpp(21): error C2872: 'cout' : ambiguous symbol

    it comes up 10 times, for each 'cout'

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It compiles fine in VC++6 if I add a main() function. Have you ever considered that all those couts and endls could be replaced by:
    Code:
    for(int i=0; i<100; i++)
    {
        cout<<endl;
    }

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    148
    It compiles with .Net 2003 (VC 7.1).

    But 100 times endl is very ugly (endl == '\n' + flush)
    Code:
    for(int i=0; i<100; i++)
    {
        cout<< '\n';
    }
    cout.flush();

  4. #4
    Unregdperson
    Guest
    I ended up making it a function instead of a class

    I don't know why .NET won't compile it though *shrugs*

    Yeah, in my function I added the loop instead of all the couts
    I didnt worry about the number of couts in the class, but seeing as its now a function, i didnt want a heap of useless lines added

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It works just fine for me in VS .NET 2002.

    It sounds like somewhere, through some other #include, that you are ALSO including the deprecated iostreams. For example, I get exactly those errors if I did:

    #include <iostream>
    #include <iostream.h>

    because it doesn't know the difference between std::cout and the global ::cout; both are now in the global namespace.

    I'd wager $50 that somewhere, you #include some header that #includes the deprecated iostreams.

    Check your output window (not the task window), I bet you ALSO got a warning:

    : warning C4995: '_OLD_IOSTREAMS_ARE_DEPRECATED': name was marked as #pragma deprecated
    Last edited by Cat; 06-07-2003 at 01:52 PM.

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    .net enterprise architect compiled it fine

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Any compiler, VC .NET included, would compile it fine; he has an error in the #includes that aren't shown. His error is that there are two "cout"s declared globally, and the compiler doesn't (and can't) know which to use.
    Last edited by Cat; 06-07-2003 at 04:30 PM.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You should have using namespace std; right after the #include, not in a function.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It's OK to put it in a function; it limits the scope of "using" to just that function (which is often better, especially if you're dragging the entirety of std:: into there).

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Cool. I never knew that.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  2. Class Errors (LNK2005)
    By NMZ in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2005, 03:52 PM
  3. errors in my class....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 03:22 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM