相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have multiple classes in my program.

A) When I create an object of a class in another class I am getting no error but when I use the object to call a function I get the above error.

B)Also if I create an object of another class and call a function using that in the constructor of my class then I get no error like this.

C) Cout function does not work in the body of the class except when I put it any function

D) The main class is able to do all of these and I am not getting any error.

It would be great to hear back soon. Thank you in advance.

Following is the code : These are two classes in my cpp. I am facing no problems except using object after creating it. the code is too huge too be posted. Everything can be done in main but not in other classes why?

 #include <iostream>
 #include <fstream>
 #include <iomanip>
 #include <string>
 #include <cstdlib> 
 #include <vector>
 #include <map>
 using namespace std;
 class Message
    public:
    void check(string side)
       if(side!="B"&&side!="S")
           cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
class Orderbook
    public:
      string side;
      Orderbook()      //No Error if I define inside constructor
      Message m;       //No Error while declaring
      m.check(side);   //Error when I write m. or m->
                Sounds like you're putting expressions where only declarations can appear. It would be so much easier to help if you showed us your code...
– jrok
                Apr 6, 2014 at 20:19
                Please edit the code indenting style for more consistency.  Is the using namespace std; somewhere above?  Are the two classes inside a header or cpp? What header files are being included?
– CPlusPlus OOA and D
                Apr 6, 2014 at 21:01
                This code is wrong in so many ways I'd just tell you to learn more about programming in C++ before trying to achieve something like that. I've properly indented your code.
– Chnossos
                Apr 6, 2014 at 21:17
                I have edited and provided additional info. Can you tell me some of the so many mistakes you see ?
– Jain
                Apr 6, 2014 at 21:22

That code has to go inside a function. Your class definition can only contain declarations and functions.

Classes don't "run", they provide a blueprint for how to make an object.

The line Message m; means that an Orderbook will contain Message called m, if you later create an Orderbook.

Thank you so much . I was calling a function in the class defination. It works fine inside a function . – Jain Apr 6, 2014 at 21:36 @Jain neither classes, class declarations, nor class definitions run . Although I guess this all depends on your definition of "run" – M.M Apr 29, 2020 at 4:06

Calling m.check(side), meaning you are running actual code, but you can't run code outside main() - you can only define variables.

In C++, code can only appear inside function bodies or in variable initializes, unless it's a function that initializes a static variable.

You can run code outside main just fine if it's a function that initializes a static variable. – StoryTeller - Unslander Monica Jul 17, 2017 at 8:55

You can declare an object of a class in another Class,that's possible but you cant initialize that object. For that you need to do something like this :--> (inside main)

Orderbook o1;
o1.m.check(side)

but that would be unnecessary. Keeping things short :-

You can't call functions inside a Class

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章