Results 1 to 6 of 6

Thread: whats wrong with this

  1. Default whats wrong with this

    i was messing about writing some simple code to add years to ages

    so it would add 10 to 25 and out come in cout as 35


    but befor i could do anything i keep getting a error

    line of code follows

    #pragma once
    #include <iostream>

    class Nicky
    {
    int nicky;
    nicky = 25;

    };

    the bit in bold keeps coming p that i have not declared its type or storage space but i told it to be a int on the first line
    any ideas why this is wrong
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  2. #2

    Default

    My coding expertise is virtually non-existent, but moving int nicky; to the top, in the start bit (or above that?) may help.
    "Add a dab of lavender to your milk, leave town with an orange, and pretend you're laughing at it."

  3. Default

    tried this but still not getting any where it needs to be in the class between { }; to initalise the int iv'e tried moving out side the class specs but it still gives me the same problem donno what to do
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  4. #4
    Join Date
    Nov 2009
    Location
    West Yorkshire
    Posts
    292

    Default

    The assignment 'nicky = 25' needs to go into a constructor as you're using a class, you're declaring a custom object, not directly running code like you would a function. This isn't something that can be explained easily, so I wrote an example that does what you said using a class object, hopefully with that you'll be able to see for yourself where you went wrong.

    Code:
    #include <iostream>
    #include <string>
    
    //Person class declaration
    class Person
    {
    public:
    	Person(const char* name)
    	{
    		m_name.assign(name);
    		m_age = 25;
    	}
    
    	void AddToAge(int ageIncrease)
    	{
    		m_age += ageIncrease;
    	}
    
    	void DisplayAge()
    	{
    		std::cout << "Age Of " << m_name << " Is: " << m_age << std::endl;
    	}
    
    private:
    	std::string m_name;
    	int m_age;
    };
    
    //Main Loop ----
    int main()
    {
    	Person m_nicky("Nicky");
    
    	m_nicky.AddToAge(10);
    	m_nicky.DisplayAge();
    
    	//Pause
    	std::cin.get();
    	return 0;
    }

  5. Default

    thank you that clears it up a little lol i'm new to coding i moved from the designers course and getting confused with this quite easy i'm still trying to learn a lot about c++ and coding
    The pain of being alone...is not an easy one to bear..why is it...that I can understand your pain?
    But...I already have many people who are important to me now...and I can't let you hurt any of them...even if I have to kill you!

  6. #6

    Default

    You can't assign a value to your member variables this way unles you do it in one line

    int nicky = 25; will work but as mentioned before the best way to do it is ether in constructor or in member functions
    btw. There is a Facebook group you can join if you want and as questions there it is called Train2Game Students it is a closed group but admin will add you with no problem
    Last edited by rafalmlody12321; 01-25-2016 at 01:06 AM.
    This course is a pain in the as??????? No one said it gonna be a peace of cake


Similar Threads

  1. Wrong CD ?
    By CoMstantin in forum T2G Artist and Animator, General Discussions
    Replies: 0
    Last Post: 07-15-2012, 10:16 PM
  2. What am i doing so wrong..
    By CreateNPlay in forum T2G Artist and Animator, General Discussions
    Replies: 7
    Last Post: 05-21-2012, 02:40 PM
  3. SMA's Is it wrong that I..........
    By Retrosp3k in forum T2G Designer, General Discussions
    Replies: 10
    Last Post: 12-09-2011, 09:05 PM
  4. Placed on wrong course
    By Steve "0.1" Lee in forum Newbies Area
    Replies: 8
    Last Post: 04-11-2010, 02:21 AM
  5. wrong course?
    By oOASHOo in forum T2G Designer, General Discussions
    Replies: 16
    Last Post: 07-01-2009, 01:50 AM

Posting Permissions

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