Thread: Need some help with C++ object oriented programing

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    california
    Posts
    8

    Need some help with C++ object oriented programing

    k so heres my problem im writing a program that has a class called String and this will take a string and remove the null charicter and it will have a varible for the length of the string.

    im using Dev C++ as a compiler, yeah probly not the best but its free.

    the errors im getting are when i start to overload ostream.
    heres what the compiler says:

    11 main.cpp In file included from main.cpp
    121 My_String.h ISO C++ forbids declaration of `ostream' with no type
    121 My_String.h `ostream' is neither function nor member function;
    cannot be declared friend
    121 My_String.h expected `;' before '&' token
    125 My_String.h ISO C++ forbids declaration of `istream' with no type
    125 My_String.h `istream' is neither function nor member function;
    cannot be declared friend
    125 My_String.h expected `;' before '&' token
    G:\CISP 430\Project2\Makefile.win [Build Error] [main.o] Error 1

    and now the main part of the code
    first the .h file
    Code:
    class String
    {
        
        char* STR; //string
        unsigned Len; // length of string
        
    public
        friend ostream& operator<< (ostream& , const String&);
    
        friend istream& operator>> (istream& , const String&);
    now the .cpp file
    Code:
    ostream& operator<<(ostream& Os, const String& s)
    {
        for(unsigned I=0; I < s.Len;I++)
        {
            Os<<s[I];
        }
        return (Os);
    }
    
    istream& operator>>(istream& Is, const String& s )
    {
        String Temp;
        char cc;
        while (is.get(cc))
        {
            if (cc=='\n')
            {
                break;
            }
        }
        Temp +=cc;
        s = Temp;
        return Is;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're not including the standard headers for istream and ostream, and/or not getting them into the correct scope.

    What tutorial are you going by? This code has many other errors, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    california
    Posts
    8
    well thats not all my code. but just to make sure i have all the right headers in my files here they are these are just the headers in each file.

    Main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "My_String.h"
    
    using namespace std;
    My_String.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "My_String.h"
    
    using namespace std;
    My_String.h
    Code:
    #include <iostream>
    
    using namespace std;
    and if you would like to see all code just let me know
    thank you.
    Last edited by akairyuu; 09-26-2009 at 02:45 AM. Reason: for got code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good Object Oriented Strategy for OpenGL/Graphics
    By indigo0086 in forum Game Programming
    Replies: 9
    Last Post: 04-03-2007, 06:27 PM
  2. object oriented C
    By FlatLost in forum C Programming
    Replies: 4
    Last Post: 11-08-2005, 06:22 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

Tags for this Thread