C++ newbie program full of errors!

This is a discussion on C++ newbie program full of errors! within the C++ Programming forums, part of the General Programming Boards category; Hi! I tryed to write down some code of C++, but i have some problems... ...actually i am in my ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    C++ newbie program full of errors!

    Hi! I tryed to write down some code of C++, but i have some problems...
    ...actually i am in my first 5 programs in C++ so i may have stupid mistakes...

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Data{
    	private:
    		int age;
    		char name;
    	public:
    		init();
    		printAll();
    };
    
    void init(class Data info)(
    	info.age = 20;
    	info.name = "Vasilis";
    }
    
    int main(void){
    	Data info;
    		cout << "Hello there! \n";
    		init(info);
    		// printAll(info); *i will declare this later! 
    return 0;
    }
    and my output is:
    Code:
    program.cpp:12: error: ISO C++ forbids declaration of ‘init’ with no type
    program.cpp:13: error: ISO C++ forbids declaration of ‘printAll’ with no type
    program.cpp:17: error: function ‘void init(Data)’ is initialized like a variableprogram.cpp:17: error: ‘info’ was not declared in this scope
    program.cpp:18: error: expected constructor, destructor, or type conversion before ‘.’ token
    program.cpp:19: error: expected declaration before ‘}’ token
    so, can anyone help me please...? thanks in advance...

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    When you declare a function or method, it must have a return type.
    When you implement a class method outside of the class definition, the method name must be prefixed with the class name, i.e.,
    Code:
    void Data::init()
    Your method implementation must also exactly match the method definition.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by rags_to_riches View Post
    When you declare a function or method, it must have a return type.
    When you implement a class method outside of the class definition, the method name must be prefixed with the class name, i.e.,
    Code:
    void Data::init()
    Your method implementation must also exactly match the method definition.
    so, if i want to "call" the function init from main i would write something like yours, right?

    edit:
    you mean something like this one?

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Data{
    	private:
    		int age;
    		char name;
    	public:
    		int init();
    		void printAll();
    };
    
    int Data::init(void)(
    	age = 20;
    	name = "Vasilis";
    }
    
    void Data::printAll(void){
    	cout << "Hi!\n";
    }
    
    int main(void){
    	Data info;
    		cout << "Hello there! \n";
    		info.init();
    		info.printAll(); //*i will declare this later! 
    return 0;
    }
    Last edited by brack; 10-15-2010 at 07:02 AM.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Here is a version of the code that should work
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Data{
    	private:
    		int age;
    		char name;
    	public:
    		void init();
    		void printAll();
    };
    
    void Data::init(){
    	age = 20;
    	name = "Vasilis";
    }
    
    void Data::printAll(){
                    cout << age "\n";
                    cout << name "\n";
    
    
    int main(void){
    	                Data info;
    		cout << "Hello there! \n";
    		info.init();
    		info.printAll(); // i will declare this later! 
    return 0;
    }
    Hope it helped!!!

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    In init function there is this error...

    18 C:\Users\cs091770\Desktop\version.cpp invalid conversion from `const char*' to `char'

    In printAll function there is this error...

    22 C:\Users\cs091770\Desktop\version.cpp expected `;' before string constant
    23 C:\Users\cs091770\Desktop\version.cpp expected `;' before string constant

    Can anyone correct them...?

    edit: Just to know i added the "}" that missed!!

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    This is what happens when you accept code from morons who've only posted like three times and only like doing people's homework (badly). Let's fix what you wrote:

    Quote Originally Posted by brack View Post
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Data{
    	private:
    		int age;
    		char name;
    	public:
    		int init();
    		void printAll();
    };
    
    int Data::init(void)(
    	age = 20;
    	name = "Vasilis";
    }
    
    void Data::printAll(void){
    	cout << "Hi!\n";
    }
    
    int main(void){
    	Data info;
    		cout << "Hello there! \n";
    		info.init();
    		info.printAll(); //*i will declare this later! 
    return 0;
    }
    Compiling this I get
    Compiling: foo.cpp
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp:16: error: initializer provided for function
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp:18: error: expected constructor, destructor, or type conversion before '=' token
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp:19: error: expected declaration before '}' token
    Process terminated with status 1 (0 minutes, 1 seconds)
    3 errors, 0 warnings
    So what does this mean? Well you usually take it one error at a time. The first error is on line 16, and it says initializer provided for function. Whatever we typed, it is being interpreted as an initializer by the compiler. Except we know that init is a function and not a variable that could be initialized. To fix this, we have to scrutinize the syntax (as we always should). My editor highlights
    Code:
    void Data::init(void)(
    Obviously this parens was meant to be a bracket, so we replace that and recompile.

    Now we get
    Compiling: foo.cpp
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp: In member function 'int Data::init()':
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp:18: error: invalid conversion from 'const char*' to 'char'
    C:\Documents and Settings\Owner\My Documents\foo\foo.cpp:19: warning: no return statement in function returning non-void
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 1 warnings
    Now we need to fix both of these. The error would be fixed by changing char name to const char *name, but is that appropriate? By the way name is used, it looks like it is. So we change that and recompile. And if done correctly, you are left with the warning "no return statement in function returning non-void".

    That is only related to the init function, as the line number implies. This function looks like it should be a void function to me, so I would change the return type, but you can also return an int if you want.

    And then we have correct code! You did it yourself, and learned a lot, I hope.
    Last edited by whiteflags; 10-16-2010 at 10:15 AM.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i did it!!! thank you so much whiteflags!!!
    i sincerely aπpreciate your help!!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I think it is more appropriate that name be a std::string. That way, you can assign and modify it. std::string is, put simply, a C++ string.
    It should also avoid pointer pitfalls.
    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.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by Elysia View Post
    I think it is more appropriate that name be a std::string. That way, you can assign and modify it. std::string is, put simply, a C++ string.
    It should also avoid pointer pitfalls.
    It would be more worth it if brack was actually doing string operations. As it is, he can assign to name as many times as he wants and print it.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    True that, but I figured it would scale better. Better to learn something that is works all the time and is safe than something that can be error prone and works only half the times.
    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.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    C++ is newbie to be! You may be right Elysia, i am now learning C++ (tips) so i missed it! i' ll try modify this...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 04:52 AM
  2. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. newbie: steps to create and compile c program in SUSE
    By gemini_shooter in forum Linux Programming
    Replies: 12
    Last Post: 06-22-2005, 06:35 PM

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