Why won't this work!?! Arggg lol

0 Jordan Friend · July 8, 2015
#include <iostream>
#include <string>

using namespace std;

class BuckysClass;

int main()
{
    BuckysClass buckysObject;
    buckysObject.coolSaying();
    return 0;
}

class BuckysClass
{
    public:
        void coolSaying()
        {
            cout << "Preaching to the choir" << endl;
        }

};

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jordan Friend · July 9, 2015
Hey Adam! Thanks for your help! I am aware that code is read from top to bottom. I had tried putting the class above like you did here, however I have OCD when it comes to keeping main at the top. Is there any way I can reference the class above main like you can a function and keep the class below the main?
+1 Violina Nath · July 10, 2015
you have to define the class above the main() function as C++ is a bottom to top procedure, so when you are creating an object it shows error. 


#include <iostream>
#include <string>

using namespace std;

class BuckysClass;
class BuckysClass
{
public:
void coolSaying()
{
cout << "Preaching to the choir" << endl;
}

};



int main()
{
BuckysClass buckysObject;
buckysObject.coolSaying();
return 0;
}
0 Adam Tindall · July 8, 2015
// The New Boston //
// 11. Functions That Use Multiple Parameters //



#include <iostream>

using namespace std;


class BuckysClass  {
// Again programs read/run from top to bottom.
// "Access specifier" put in "public" so main can access it (below)

public:
// This allows main to access all the functions within the class
  void coolSaying(){


    cout << "preaching to the choir"  << endl;

  }

  };


int main()

{

  BuckysClass buckysObject;  // In main you create an object to access the variables, functions, etc. in "BuckysClass"
  buckysObject.coolSaying();  //Now in the program it knows to create an object out of buckys class to use in the main program

  cin.ignore().get();
  return 0;
}
0 Adam Tindall · July 8, 2015
So looking at it it looks that where you have public located my be your problem.  

That is a working program so you can just drop the code in and I gave some line by line pointers too. Let me know if this helps/have other questions.  I think if you move it around a bit you will see it too :)  

Adam
0 Adam Tindall · July 8, 2015
#include <string> lib is not necessary in this case as well.  just fyi
0 Adam Tindall · July 13, 2015
Hey Jordan, sure thing! Sorry been camping in Alabama no with no connection. Glad to be of service.  I just included my program with all notes just in case for your reference :)  So to your second question, to my knowledge, Vinolia has it right.  I'll def think about it though.  I've notice in my experience in coding I have had to give up a lot of my OCD regarding the IDE since we have to start with some parameters so we can talk about something lol.  And someone else made those rules.  Not sure who.  You're OCD, like mine, will probably be a huge benefit going forward if you keep patient. Since coding is about being exactly precise.  Like I was saying kinda go with the flow which I've noticed is slow but once ya get it is just the right pace.    Hope this helps I don't know anything for sure so I'll dwell on the question for a bit while I code today.

Kind regards,

Adam
0 Adam Tindall · July 13, 2015
It's just letting the comp know what to expect in main function and where to reference if it comes across as it reads.  When Vinolina says "bottom up" is just saying that build small first into larger program.  Have to notify the comp whats up prior to main.  So like to explain in human terms Kinda got to go to "class" before you know the functions if that makes more sense.  Unless you want to fail your math test ;) haha
  • 1

C++

107,011 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator