CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Location
    USA,PA
    Posts
    26

    first foray into classes; need a bit of help

    The assignment is to write our own string function; not a big deal. However I dont think that I am declaring everything correctly as I am getting 20-some nearly identical errors that I cant get to go away. Any insight into the issue here would be greatly appreciated.

    mystring.h
    Code:
    #include <iostream>
    using namespace std;
    
    
    class String
    {
    public:
    	void string();
    	void assign(const char s[]);
    	void append(const String &str);
    	int compare_to(const String &str);
    	void print() const;
    	int length() const;
    	char element(int i);
    
    private:
    	int lngth;
    	char strng[100];
    }
    mystring.cpp
    Code:
    #include "mystring.h"
    #include <iostream>
    using namespace std;
    
    
    void String()
    {
    
    	for (int i=0;i<100;i++)
    	{
    		strng[i]='\0';
    	}
    }
    void assign(const char s[])
    {
    	lngth=0;
    	for (int i=0;i<100;i++)
    	{
    		strng[i]=s[i];
    		lngth++;
    	}
    
    }
    void append(const String &str)
    {
    	int i,n;
    	for (i=0;i<100 && strng[i]!='\0';i++)
    	{
    	}
    	for (n=0; *(str+n)=!'\0'; n++,i++)
    	{
    		strng[i]=*(str+n);
    		lngth++;
    	}
    
    }
    int compare_to(const String &str)
    {
    	int i, n, check=0;
    	for (i=0;(i<100 && strng[i]!='\0')&&(n<100 && *(str+n)!='\0')&& check=0;i++)
    	{
    		if (strng[i]<*(str+n))
    			check=-1;
    		if (strng[i]>*(str+n))
    			check=1;
    	}
    	return check;
    
    
    
    }
    void print() const
    {
    	for (int i=0; strng[i]!='\0';i++)
    		cout<<strng[i];
    
    }
    int length() const
    {
    	return lngth;
    
    }
    char element(int i)
    {
    	if (i<100 && strng[i]=!'\0')
    		return strng[i];
    	else
    		cerr<<"Element out of range";
    		return '\0';
    }

    ....error messages
    Code:
    1>mystring.cpp
    error C2143: syntax error : missing ';' before 'using'
    (11) : error C2065: 'strng' : undeclared identifier
    (16) : error C2065: 'lngth' : undeclared identifier
    (19) : error C2065: 'strng' : undeclared identifier
    (20) : error C2065: 'lngth' : undeclared identifier
    (24) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (24) : error C2143: syntax error : missing ',' before '&'
    (27) : error C2065: 'strng' : undeclared identifier
    (30) : error C2065: 'str' : undeclared identifier
    (32) : error C2065: 'strng' : undeclared identifier
    (32) : error C2065: 'str' : undeclared identifier
    (33) : error C2065: 'lngth' : undeclared identifier
    (37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    (37) : error C2143: syntax error : missing ',' before '&'
    (40) : error C2143: syntax error : missing ';' before 'for'
    (40) : error C2065: 'strng' : undeclared identifier
    (40) : error C2065: 'str' : undeclared identifier
    (42) : error C2065: 'strng' : undeclared identifier
    (42) : error C2065: 'str' : undeclared identifier
    (44) : error C2065: 'strng' : undeclared identifier
    (44) : error C2065: 'str' : undeclared identifier
    (53) : error C2270: 'print' : modifiers not allowed on nonmember functions
    (54) : error C2065: 'strng' : undeclared identifier
    (55) : error C2065: 'strng' : undeclared identifier
    (59) : error C2270: 'length' : modifiers not allowed on nonmember functions
    (60) : error C2065: 'lngth' : undeclared identifier
    (65) : error C2065: 'strng' : undeclared identifier
    (66) : error C2065: 'strng' : undeclared identifier
    the above errors were all attributed to mystring.cpp. The driver file was provided, and I am confident It it error-free.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: first foray into classes; need a bit of help

    Code:
    void String::String()
    {
    Same thing for the other functions in class String

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: first foray into classes; need a bit of help

    Make sure the constructor name is capitalized in the h file, too.

    While I don't know the specific requirements of your assignment, I'll point out that using a fixed-size char array to store the underlying string severely limits the usefulness of a string class. Perhaps a future assignment will be to overcome that limitation?

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: first foray into classes; need a bit of help

    your header file is missing the semicolon after the definition of your class, that is the first error your compiler complains about.
    and if
    Code:
    void string();
    should be the constructor of your class, use a capital 'S' as Lindley suggested and omit the 'void'.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: first foray into classes; need a bit of help

    Quote Originally Posted by jay5965 View Post
    mystring.h
    Code:
    #include <iostream>
    using namespace std;
    It's bad style to put a using namespace directive in a header file. This way the directive will be put in every file that includes this header, effectively eliminating the advantages of a namespace.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Feb 2010
    Posts
    4

    Re: first foray into classes; need a bit of help

    You don't need to include iostream at any rate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured