#include <iostream>
using namespace std;
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
I am getting the following compilation error with g++:
l1.cpp: In function 'int main()':
l1.cpp:5:15: error: 'HelloWorld' was not declared in this scope
You need to either declare or define the function before you can use it. Otherwise, it doesn't know that HelloWorld() exists as a function.
Add this before your main function:
void HelloWorld();
Alternatively, you can move the definition of HelloWorld() before your main():
#include <iostream>
using namespace std;
void HelloWorld()
{
cout << "Hello, World" << endl;
}
int main()
{
HelloWorld();
return 0;
}
You must declare the function before you can use it:
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
or you can move the definition of HelloWorld() before main()
You need to forward declare HelloWorld() so main knows what it is. Like so:
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
There is one more possibility for some reason nobody mentioned, namely using extern declaration:
#include <iostream>
using namespace std;
int main()
{
extern void HelloWorld();
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
It's preferable when you don't want to introduce name of the function into namespace scope.
You need to have either a prototype of the function before the invocation or the whole function before it.
So the first is:
void HelloWorld();
int main() {
HelloWorld();
return 0;
}
void HelloWorld() {
cout << "Hello, World" << endl;
}
And the second way is:
void HelloWorld() {
cout << "Hello, World" << endl;
}
int main() {
HelloWorld();
return 0;
}
All these answers are correct, but strangely enough, this would have worked:
struct Hello {
static int main() { World(); return 0; } /* note: World() not declared yet */
static void World() { std::cout<<"Hello World"; }
};
int main() { return Hello::main(); }
You have to put the function before the main function.
in C++ you need to define or at least declare the functions before calling them.
What you are trying to do till now is something like this :
int main()
{
cout << b;
int b = 10;
}
So you can also trying like this :
#include <iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
It is a good practice in C++ to define all other functions before the main function.
Rearrange HelloWorld() so that it appears before main():
#include <iostream>
using namespace std;
void HelloWorld()
{
cout << "Hello, World" << endl;
}
int main()
{
HelloWorld();
return 0;
}
If you're defining you're functioning below your main function you should declare it above first.
#include<iostream>
using namespace std;
void HelloWorld();
int main()
{
HelloWorld();
return 0;
}
void HelloWorld()
{
cout << "Hello, World" << endl;
}
Declare void HelloWorld() above main() method or add function prototype void HelloWorld(); so compiler can compile it . if the prototyped function does not exist then you get a linker error.
in this case
you are trying like this
std::cout <<hello<<endl;
std::string hello = "hello world";
Related
i have 2 .h files:
first.h
namespace first
{
void foo() { std::cout << 1 << ","; }
void bar() { std::cout << 7; }
}
second.h
namespace second
{
void foo() { std::cout << 3 << ","; }
void bar() { std::cout << 9; }
}
and the main.cpp
#include <iostream>
#include "first.h"
#include "second.h"
......................
void main() {
foo();
bar();
}
The goal is that the main prints 3,7 using these h files above.
Does someone have an idea how I could do it? Thanks
You can call the functions using the namespace:
second::foo();
first::bar();
But if you want to use the same functions over and over you can bring them into your scope via
using second::foo;
using first::bar;
foo();
bar();
I'm currently learning c++ for a week and here's my problem:
run.cpp
#include <iostream>
#include "Abc.h"
int main(){
int a;
std::cout << "Enter a : ";
std::cin >> a;
// Object Initialization
Abc AbcObj();
}
the header, Abc.h :
#ifndef ABC_H
#define ABC_H
class Abc
{
public:
Abc();
protected:
private:
};
#endif // ABC_H
and finally my cpp file for implementation, Abc.cpp:
#include "Abc.h"
#include <iostream>
Abc::Abc()
{
std::cout << std::endl << "Object created ";
}
Why don't I get output on my console? I'm expecting "object created" should be on the console. These files are in the same directory.
You error doesn't come up because you've used different files, so I have used one in this example
struct Foo
{
int a;
Foo()
{
std::cout << "Constructor called!";
}
};
int main()
{
Foo obj();
}
Why don't you see the message? You can read this thread
The problem here is, Foo obj() is taken as a function declaration. To fix this you need to remove the ()
int main()
{
Foo obj;
}
Constructor called!
I have a compiling error in C++ using classes. I have worked with classes before and have never encountered this error. I have tried adding static before the method ImprtData but that only prompted more errors.
error: invalid use of non-static member function bank.ImprtData;
here is my .cpp
#include "componets.h"
User::User() {
std::cout << "loaded" << std::endl;
}
void User::ImprtData() {
std::cout << "loaded.\n";
}
and here is my .h
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
class User {
public:
User();
void write();
void launch_main_menu();
void login();
void ImprtData();
private:
void deposit();
void withdrawl();
std::string account_name;
int account_pin;
float account_balance;
std::string account_user_name;
};
and finally my main
#include "componets.h"
int main() {
std::cout << "Welcome to Bank 111.\n";
User bank;
bank.ImprtData;
return 0;
}
This is essentially a simple typo. Replace
bank.ImprtData;
with
bank.ImprtData();
to call the function. The expression bank.ImprtData is confusing the compiler since it's interpreting it as the address of a function, and issues a diagnostic since the function is not static.
bank.ImprtData; should be bank.ImprtData();
Good evening! (morning?)
I was wondering if anyone is familiar with the following issues. There are three files here, which are Cat.cpp, Cat.h, and CatMain.cpp. The issues are as follows:
When I try to build Cat.cpp, I get the error "undefined reference to WinMain#16".
When I try to build CatMain.cpp, I get undefined reference errors for the speak and jump functions.
The files are in the same folder and the code is just one-liners:
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
Cat.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif // CAT_H
CatMain.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}
Is there anything wrong with this code? Or is anyone aware of whether or not this is a Code::Blocks or a compiler issue?
Any help is greatly appreciated =)
All that your code does as of now is something like this :
Cat.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
void speak()
{
cout << "meow" << endl;
}
void jump()
{
cout << "meow?" << endl;
}
CatMain.cpp ::
#include <iostream>
using namespace std;
void speak();
void jump();
int main()
{
speak();
jump();
return 0;
}
You Cat.cpp has a main method missing because of which it wouldn't compile.
Your CatMain.cpp does not have any definition for speak() and jump(), hence undefined error.
Point : CatMain.cpp doesn't know what Cat.cpp is trying to work out.
int main() { return 0; }
added to Cat.cpp should let it compile.
void speak(){
cout << "defined" << endl;
}
void jump(){
cout << "defined" << endl;
}
added to CatMain.app should work for it as well.
Your code is fine.
Most probably you don't want to compile separate files but project as whole. You should add both cpp to project in your IDE and linker will link them together resolving both issues.
Below is my code
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
using namespace std;
class ToDoCommands
{
public:
void getCommand(string);
};
void ToDoCommands::getCommand(string command)
{
cout<<command; //here i get ping
void (*CommandToCall)(void);
CommandToCall = command; // error here i want something like
// CommandToCall = ping
CommandToCall();
}
void ping(void)
{
cout<<"ping command executed";
}
int main()
{
ToDoCommands obj;
obj.getCommand("ping");
}
The function pointer should refer to function ping dynamically. A string same as function name is passed to getCommand function in main.
C++ just doesn't work that way. If you really need something like that, you'll have to make a table of functions that are indexed by name:
#include <assert.h>
#include <iostream>
#include <map>
#include <string>
using std::cout;
using std::string;
using std::map;
void ping(void)
{
cout << "ping command executed\n";
}
class ToDoCommands
{
public:
typedef void (*FunctionPtr)();
typedef string Name;
void registerFunction(Name name,FunctionPtr);
void callFunction(Name);
private:
map<Name,FunctionPtr> func_map;
};
void ToDoCommands::registerFunction(Name name,FunctionPtr func_ptr)
{
func_map[name] = func_ptr;
}
void ToDoCommands::callFunction(Name name)
{
assert(func_map.find(name)!=func_map.end());
func_map[name]();
}
int main(int argc,char **argv)
{
ToDoCommands to_do_commands;
to_do_commands.registerFunction("ping",ping);
to_do_commands.callFunction("ping");
return 0;
}
void ping(void)
{
// LL DD…DD XX
cout<<"ping command executed"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)(void)); //getCommand(ping)
};
void ToDoCommands::getCommand( void (*CommandToCall)(void) )
{
void (*CommandToCall1)(void);
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
ToDoCommands obj;
obj.getCommand( ping );
return 0;
}
i tried this and its working :)