Newbie class question

This is a discussion on Newbie class question within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I have this class that is giving me errors and I cannot figure out why, it seems pretty ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Newbie class question

    Hello everyone. I have this class that is giving me errors and I cannot figure out why, it seems pretty simple. Here is the code:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class myString
    {
        int x;
        public:
            void set_value(int);
            int display_x() { return x; }
    }
    
    void myString::set_value(int a) {
        x = a;
    }
    
    int main() {
        myString myStr;
        myStr.set_value(22);
        cout >> "Value: " + myStr.display_x();
        system("Pause");
        return 0;
    }
    And here is the error:

    14 N:\USERS\bakewens\CS240\test2.cpp new types may not be defined in a return type
    14 N:\USERS\bakewens\CS240\test2.cpp two or more data types in declaration of `set_value'
    14 N:\USERS\bakewens\CS240\test2.cpp prototype for `myString myString::set_value(int)' does not match any in class `myString'
    10 N:\USERS\bakewens\CS240\test2.cpp void myString::set_value(int)
    14 N:\USERS\bakewens\CS240\test2.cpp `myString myString::set_value(int)' and `void myString::set_value(int)' cannot be overloaded
    N:\USERS\bakewens\CS240\test2.cpp In function `int main()':
    21 N:\USERS\bakewens\CS240\test2.cpp "))'
    I'm not sure why I am getting these - I copied and pasted a very similar program that works just fine, and it looks almost exactly the same. Any ideas? Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class myString
    {
        int x;
        public:
            void set_value(int);
            int display_x() { return x; }
    }; // notice the semicolon
    
    void myString::set_value(int a) {
        x = a;
    }
    
    int main() {
        myString myStr;
        myStr.set_value(22);
        cout << "Value: " << myStr.display_x();
        system("Pause");
        return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 02:01 PM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 09:45 AM
  5. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 05:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21