This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
It looks basic and it’s probably due to a beginner mistake but I can’t figure out why...
When compiling I get an error like below from int main():
“undefined reference to 'Hello::World::PaintService::PaintService()"
paint.cpp
using namespace Hello;
int main(int argc, char **argv) {
World::PaintService service;
service.start_painting(argc[1]);
}
PaintService and start_painting are defined like below:
paint_service.h
namespace Hello {
namespace World {
class PaintService {
public:
PaintService();
void start_painting(...);
}; } }
paint_service.cpp
namespace Hello {
namespace World {
void start_painting(....) {
... //paint
}
} }
It seems simple to call that start method like service.start_paint() in another class after calling PaintService service, but something is wrong. I have tried lots of variations yet couldn’t figure out :-/ Could someone point out what I’m doing wrong?
Thanks!
To declare a method, you need to include the name of the class.
namespace Hello {
namespace World {
void PaintService::start_painting(....) { ... }
void PaintService::PaintService() { ... }
} // namespace World
} // namespace Hello
https://repl.it/repls/PunySaneSpools#main.cpp
Related
This question already has answers here:
Injected class name compiler discrepancy
(3 answers)
Closed 6 years ago.
namespace fooo {
class Fooo {
public:
int a;
};
}
namespace fooo {
class Test {
public:
Test(Fooo::Fooo *i) {
i->a = 1;
}
};
}
This code compiles fine with clang (any version) but fails with gcc.
Can anyone explain why?
EDIT:
Yes, I know the issue here is kinda obvious but why does clang accept it? The person who told me this said that this is a bug in the standard and that there is a Defect Report. Can anyone point to the actual DR?
The error message from gcc tells you exactly what the problem is:
t.cpp:11:16: error: ‘fooo::Fooo::Fooo’ names the constructor, not the type
Test(const Fooo::Fooo *i) {
^
it is suprising that clang doesn't give an error.
This question already has answers here:
How to declare a global variable in C++
(5 answers)
Closed 7 years ago.
EDIT 2:
Solved! Use the code below and it worked!
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
Just place the code above at the top of the code. (Maybe the next line of include or namespace)
I'm using irrKlang to play audio but I had a problem:
#include <irrKlang.h>
void playSound() {
engine->play2D("src/Click.wav");
}
int main() {
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
playSound();
engine->drop();
return 0;
}
When I run this code, it show that 'engine' (that in the void) was not declared in this scope.
I test this at int main but it work. The problem is that it only worked at main but not at void.
Anything I can use to fix this error? Or is it a bug?
Thanks in advance.
That is expected. irrklang::ISoundEngine* engine is defined in main function but not in playSound().
A straightforward solution would be to pass engine as an argument
void playSound(irrklang::ISoundEngine* engine) {
engine->play2D("src/Click.wav");
}
and in main call it like this
playSound(engine);
I decided to learn C++ DirectX, but whenever I compile/debug a code, even the simplest ones, i get the LNK2019: unresolved external symbol _WinMain# 16 referenced in function "int __cdecl_main(void)" (?invoke_main##YAHXZ) error and LNK1120.
I tested two different codes, one with class and other just int function alone:
#pragma once
class Main
{
public:
Main();
~Main();
};
Main::Main(int x)
{
}
Main::~Main()
{
}
int example()
{
return 0;
}
First and foremost you should do yourself a favour and learn C++ properly from a book. Bjarne Stroustrup, the designer and implementer of C++, has a great book which will teach you lots: Programming: Principles and Practice using C++
Your program cannot link because there is no main() function, which is required.
As Steephen has pointed out already in his answer, you can change your program so it includes at least the following:
int main()
{
return 0;
}
It looks like you were trying to substitute main() with example(), but your program and and C++ program needs to have a main(), as it's the main entry point of your program. You might also like to read http://www.cplusplus.com/doc/tutorial/program_structure/
To keep you class definition intact do following changes
Main::Main() // you don't need a parameter for your constructor
{
}
int main() //Not int example() because you need a main for your program
{
return 0;
}
This question already has answers here:
get function member address
(2 answers)
Closed 8 years ago.
I am trying to use function pointer in a c++ class but got an error.
#include <cstdio>
#include <iostream>
using namespace std;
class abc{
public:
void hello(){
printf("Hello world\n");
}
abc(){
void (*hw)(void);
hw = &hello;
hw();
}
}
int main()
{
abc ab;
return 0;
}
Error
error: cannot convert ‘void (abc::*)()’ to ‘void (*)()’ in assignment
But the following code works for me which is in code base. Can anyone please help me to find out the difference?
void hello(){
printf("Hello world\n");
}
int main()
{
void (*hw)(void);
hw = &hello;
hw();
return 0;
}
Function pointers are (unfortunately) completely different from method pointers, as the error is trying to indicate. This is because the object that the method works on needs to be passed in somehow, which is what makes methods fundamentally different from functions in the first place (and obviously affects how the call is done through the pointer). Method pointers are not even always the same size (and can be significantly larger than a function pointer) when multiple/virtual inheritance comes into play.
You need to declare a method pointer, and call it on behalf of an object of the right type using one of the esoteric .* or ->* operators:
class abc {
public:
void hello(){
printf("Hello world\n");
}
abc() {
void (abc::*hw)();
hw = &abc::hello;
(this->*hw)(); // or (*this.*hw)()
}
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
In C++ I am not able to link source code file and its header files. I am keeping both files in same folder/directory. Also I am using another class which imports header file and it is the startng point of the application but when I am compiling I am getting following error message:
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0x74):
undefined reference to `Marksheet::Marksheet(std::string,
std::string)'
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0xa9):
undefined reference to `Marksheet::dispmessage()'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o:
bad reloc address 0x13 in section
`.text$_ZN9MarksheetD1Ev[__ZN9MarksheetD1Ev]'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe final link failed:
Invalid operation
E:\Education\C++ programming\collect2.exe [Error] ld returned 1 exit
status
Here Marksheet is a cpp file of which header I am making and Marksheet_Test is starting point of the application.
Can somebody help me solving this problem?
Code is as follows:
This is code for Marksheet_Test
#include "Marksheet.h"
using namespace std;
int main()
{
Marksheet obj1("Pransanjeet Majumder","IT 114 Objject Oriented programming");
obj1.dispmessage();
}
Following code is of Marksheet.cpp
#include<iostream>
#include "Marksheet.h"
using namespace std;
class Marksheet{
Marksheet::Marksheet(string cname,string instname){
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname)
{
coursename=cname;
}
void Marksheet::setinstname(string insname){
instname=insname;
}
string Marksheet::getCoursename()
{
return coursename;
}
string Marksheet::getinstname()
{
return instname;
}
void Marksheet::dispmessage()
{
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
};
Following code is of Marksheet.h header file
#include<string>
using namespace std;
class Marksheet
{
public:
Marksheet(string,string);
void setCoursename(string);
string getCoursename();
void dispmessage();
void setinstname(string);
string getinstname();
private:
string coursename;
string instname;
};
I am using DEVC++ compiler for compiling the code
You have a class Marksheet around your implementations that is unnecessary.
Change Marksheet.cpp to:
#include<iostream>
#include "Marksheet.h"
using namespace std;
Marksheet::Marksheet(string cname,string instname) {
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname) {
coursename=cname;
}
void Marksheet::setinstname(string insname) {
instname=insname;
}
string Marksheet::getCoursename() {
return coursename;
}
string Marksheet::getinstname() {
return instname;
}
void Marksheet::dispmessage() {
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
Note that there is no class in the definition file.
What you were doing was declaring a new class called Marksheet and then attempted to define it's own members without declaring them. Also your should not put you using declarations in the header files as then any class that includes them will also have to use the same declaration. This can lead to hard to find conflicts at compile time.