so I make the class which have the function as well inside, and I create the object of the class, and then I create another separate function outside of the class and main function, and I call the function in the class in my another separate function. but it does not work.
#include <iostream>
class Hello{
public:
void Print(std::string line){
std::cout << line << std::endl;
}
};
void myFunction();
int main(){
Hello World;
myFunction();
return 0;
}
void myFunction(){
World.Print("Hello");
}
It is showing this
error: 'World' was not declared in this scope|
Try this
#include <iostream>
class Hello{
public:
void Print(std::string line){
std::cout << line << std::endl;
}
};
void myFunction(Hello World);
int main(){
Hello World;
myFunction(World);
return 0;
}
void myFunction(Hello World){
World.Print("Hello");
}
There are two mistakes in your code
The non-obvious one, the access label under which you Print function is defined is private because unless specified the default label is private when using class (if struct is used then default is public). For World to be able to call Print it has to be public.
World is out of scope for myFunction because it is defined in main. A workaround is to pass the object as an argument.
As the error message says, World is not declared in the scope of myFunction.
Maybe you're after something like this:
#include <iostream>
class Hello{
public:
void Print(std::string line){
std::cout << line << std::endl;
}
};
void myFunction();
int main(){
// Hello World;
myFunction();
return 0;
}
// add World to global scope
Hello World;
void myFunction(){
World.Print("Hello");
}
The problem is that the variable World is accessed inside myFunction() but the scope is only is only a local variable inside the main function. You can pass the variable in order call the method like this:
#include <iostream>
class Hello{
public:
void Print(std::string line){
std::cout << line << std::endl;
}
};
void myFunction(Hello* World);
int main(){
Hello World;
myFunction(&World);
return 0;
}
void myFunction(Hello* World){
World->Print("Hello");
}
Pass Word to myFunction so
#include <iostream>
class Hello{
public:
void Print(std::string line){
std::cout << line << std::endl;
}
};
void myFunction();
int main(){
Hello World;
myFunction(World);
return 0;
}
void myFunction(Hello World){
World.Print("Hello");
}
Related
why gcc 9.4 not check parameters when bind a class member funtion to a std::function viriable, but check when bind a global function? here is example code, CreateRpcFun has a parameter, but Test member function print doesn't have any other parameters except this, bind print to CreateRpcFun works well, but global funtion print2 cannot, can anybody explain why?
#include <functional>
#include <iostream>
#include <string>
using namespace std;
using CreateRpcFun = std::function<void(const string &)>;
class Test {
public:
Test() : str_("nihao!") {}
// bind print to CreateRpcFun passed compile
void print() { cout << str_ << endl; }
private:
string str_;
};
class Holder {
public:
CreateRpcFun CreateRpc;
};
class Other {
public:
Other(Holder h, string str) : h_(h), str_(str) {}
void run() { h_.CreateRpc("world!"); }
private:
Holder h_;
string str_;
};
void print1(const string &str) { cout << str << endl; }
void print2() { cout << "magic" << endl; }
int main() {
Test t;
Holder h;
h.CreateRpc = std::bind(&Test::print, &t);
Other o(h, "hhhh");
o.run();
h.CreateRpc = &print1;
h.CreateRpc("test");
// h.CreateRpc = &print2; // compile error
// h.CreateRpc("test");
}
I am trying to place functions inside a struct which is part of a class (here named menu) so I can modify the struct inside of a dedicated setup cpp file (I am trying to do this so I can modify all of the functions I want in my application in a single source file instead of changing stuff in all of my cpp files):
// Menu.h
class menu
{
public:
menu();
struct pages
{
void print_page_1();
void print_page_2();
};
};
// Setup.cpp
struct menu::pages
{
void print_page_1()
{
// ...
}
void print_page_2()
{
// ...
}
};
Then I get an error when trying to call a function within my struct:
int main()
{
menu myMenu();
myMenu.pages.print_page_1(); // error: type name is not allowed
}
What does this error mean and how can I avoid it?
Thank you!
pages is the name of the struct, it's not an object. You need an object of type pages inside menu.
Otherwise, you can have static methods inside pages and call those without creating objects.
Example (live):
#include <iostream>
struct S
{
struct P
{
void print()
{
std::cout << "Hello from P!\n";
}
} p; // object of P
struct Q
{
static void print()
{
std::cout << "Hello from Q!\n";
}
};
};
int main()
{
S s;
s.p.print();
S::Q::print();
return 0;
}
Output:
Hello from P!
Hello from Q!
You need to declare a pages object in menu.
#include <iostream>
class menu
{
std::string p1 = "1";
std::string p2 = "2";
public:
struct pages
{
menu& m;
pages(menu &m):m(m){
}
void print_page_1();
void print_page_2();
} pages;
menu():pages(*this){
}
};
void menu::pages::print_page_1()
{
std::cout << m.p1;// ...
}
void menu::pages::print_page_2()
{
std::cout << m.p2;// ...
}
int main() {
menu myMenu;
myMenu.pages.print_page_1();
// your code goes here
return 0;
}
This is my A.h file
class A
{
public:
void menuChoice();
void displaystartingMenu(); //EDIT
};
This is my A.cpp file
#include "A.h"
void displaystartingMenu()
{
cout<<"Please enter your choice:";
}
void A::menuChoice()
{
displaystartingMenu();
cout<<"Hello"<<endl;
}
int main()
{
A a;
a.menuChoice();
}
i tried to put
void menuChoice();
on the top of my cpp file but it still won't compile . it gives me error
In function ‘int main()’: A.cpp:112:13: error: ‘menuChoice’ was not declared in this scope menuChoice();
How do I compile : g++ A.cpp A.h
By right I don't need to even declare the function of top of the cpp because I have already declare it in my header file and I have included in my .cpp. What went wrong here?
EDIT:
Error :
In function `A::menuChoice()':
A.cpp:(.text+0x229): undefined reference to `A::displaystartingMenu()'
Option 1: Make an A instance and call that instance's menuChoice method:
#include <iostream>
class A {
public:
void menuChoice();
};
void A::menuChoice() {
std::cout << "Hello" << std::endl;
}
int main() {
A a;
a.menuChoice();
return 0;
}
Option 2: Make menuChoice a static method and call it as A::menuChoice:
#include <iostream>
class A {
public:
static void menuChoice();
};
void A::menuChoice() {
std::cout << "Hello" << std::endl;
}
int main() {
A::menuChoice();
return 0;
}
Edit: Addressing the new problem, when you tried to define A::displaystartingMenu you wrote:
void displaystartingMenu() {
// ...
}
It must be defined like this:
void A::displaystartingMenu() {
// ...
}
menuChoice is a non static member function of class A. In order to call it you need to instantiate an object A, as follows
int main()
{
A a;
a.menuChoice();
return 0;
}
I'm new to C++ and get a beginner's mistake:
myclass.cpp: In function ‘int main()’:
myclass.cpp: 14:16: error: ‘func’ was not declared in this scope
This is the code:
#include <iostream>
using namespace std;
class MyClass{
public:
int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << func(3);
}
I hope you can help me.
int main(){
cout << func(3);
}
func is not a global function; it is a member function of the class. You need an instance of the class to access it.
For example:
int main()
{
MyClass obj;
std::cout<< obj.func(3);
}
func is a member function, so it must be invoked through an object. For example:
int main()
{
MyClass obj;
std::cout << obj.func(3); // 6
}
In your example, you treated it as a free function, so the compiler looked for a function with that name. Since it could not find it, it issued a compiler error.
func is a member function of MyClass. To call it, you need an object of MyClass type to invoke it on:
int main(){
MyClass m; // Create a MyClass object
cout << m.func(3);
}
Alternatively, you could make func a static member function, which means that it is not associated with any particular instance of the class. However, you would still need to qualify its name as belonging to the MyClass class:
class MyClass{
public:
static int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << MyClass::func(3);
}
I am trying to call the function hello which belongs to the class program1
#include <iostream>
using namespace std;
class program1
{
program1();
~program1();
hello();
}
program1::hello()
{
cout<<"hello";
}
int main()
{
program1.hello(); //Call it like a normal function...
cin.get();
}
Names inside a class are private by default.
class program1 {
public:
program1();
~program1();
void hello() ;
};
// ...
int main(int, char **) {
program1 myProgram;
myProgram.hello();
return 0;
}
Alternatively, you can invoke a method on a temporary:
int main(int, char **) {
program1().hello();
return 0;
}
but that's probably for later in the semester.
you forgot to create an object:
int main()
{
program1 p1;
p1.hello();
}
Class definition should end with ;
Secondly, you need to instantiate class to call members on it. ( i.e., creation of an object for the class )
In C++, methods should have return type.
program1::hello(); // method should have a return type.
class members and methods are private by default, which means you cannot access them outside the class-scope.
So, the class definition should be -
class program1
{
public: // Correction 1
program1();
~program1();
void hello(); // Correction 2
};
void program1::hello() // Need to place return type here too.
{
cout<<"hello";
}
Now on creation of object for class program1, it's method hello() can be called on it.
This version is edited. (make sure you include all the body of the methods)
#include <iostream>
using namespace std;
class program1
{
public: // To allow outer access
program1();
~program1();
void hello(); // The void
}; // Don't miss this semicolon
void program1::hello() // The void
{
cout<<"hello";
}
int main()
{
program1 prog; // Declare an obj
prog.hello(); //Call it like a normal function...
cin.get();
}
I noticed that you left out return type for your hello() function.
If you want to call hello() as a member function, then as suggested you should create an object to it.
program1 prog;
prog.hello();
If you want to call it without an object, the you should use static function.
class program1
{
public: // To allow outer access
program1();
~program1();
static void hello(); // The void
}
then you can call it this way:
program1::hello();
Therefore the working code should be this way:
#include <iostream>
using namespace std;
class program1 {
public:
void hello();
}; // Don't miss this semicolon
class program2 {
public:
void static hello(); // to demonstrate static function
}; // Don't miss this semicolon
void program1::hello() {
cout << "Hello, I'm program1." << endl;
}
void program2::hello() {
cout << "Hello, I'm program2." << endl;
}
int main(void) {
program1 prog1;
prog1.hello(); // Non-static function requires object
program2::hello(); // Static function doesn't
return 0; // Return 0
}