See the below-given code
#include <iostream>
using namespace std;
class Number
{
int a;
public:
Number();
Number(int num_1) {
a = num_1;
}
void print_number(void) { cout << "Value of a is " << a << endl; }
};
int main()
{
Number num_1(33), num_3;
Number num_2(num_1);
num_2.print_number();
return 0;
}
In the above code, I am having 2 constructors in the same class but when compiling it,gives me the error
ccnd0o9C.o:xx.cpp:(.text+0x30): undefined reference to `Number::Number()'
collect2.exe: error: ld returned 1 exit status
Can anyone solve this problem? and I still need the 2 constructors but without replacing num_3 with num_3() the main function.
In your class, you have declared the default constructor, but you have not defined it.
You can default it (since C++11) and you will be good to go:
Number() = default;
Otherwise:
Number() {}
As from #TedLyngmo liked post, both will behave the same, but the class will get different meaning as per the standard, though. More reads here:
The new syntax "= default" in C++11
#Jarod42's Comment as side notes: The default constructor make sense when it provide a default value to the member a. Otherwise it will be uninitialized (indeterminate value) and reading them will cause to UB.
Use this code
#include <iostream>
using namespace std;
class Number
{
int a;
public:
Number(){};
Number(int num_1)
{
a = num_1;
}
void print_number(void) { cout << "Value of a is " << a << endl; }
};
int main()
{
Number num_1(33), num_3;
Number num_2(num_1);
num_2.print_number();
return 0;
}
Related
I encountered a problem while initializing a pointer data member i.e int* apex; inside a constructor
having parameter as int i = 0; as *apex = i;
but unfortunately nothing is executed after compiler strikes this line.
#include <iostream>
using namespace std;
class base{
int *apex;
public:
explicit base(int i = 0){
cout << "this does executes" << endl;
*apex = i; // <<<<<--- problem???
cout << "this doesnt executes" << endl;
}
};
int main(void){
base test_object(7);
cout << "this also doesnt executes";
}
// I know how to avoid this but i want to know what
// exactly the problem is associated with *apex = i;
THANKS IN ADVANCE
note-no error is generated
What you wrote is equivalent to:
int *apex;
*apex = 42;
which is undefined behavior (UB), which includes that the compiler might just include code to stop execution or to start playing the song Never Gonna Give You Up by Rick Astley.
Even
int *apex = nullptr;
*apex = 42;
would be UB because the int* pointer has to point to a valid int when dereferencing via *
Just write
class base{
int apex{};
public:
explicit base(int i) : apex(i){}
};
And be done for
I got it. Trust me I am ashamed of myself after this silly doubt.
#include <iostream>
using namespace std;
class base{
int *apex;
public:
explicit base(int i = 0){
apex = new int;
// this is what i was supposed to do
*apex = i;
}
};
int main(void){
base test_object(7);
}
Your pointer points to invalid address you didn't initialize it
This will fix what you have asked to be done.
using namespace std;
class base{
int *apex{nullptr};
public:
explicit base(int& i ): apex{&i} {
cout << "this does executes" << endl;
cout << "this doesnt executes" << endl;
}
};
int main(void){
int a = 7
base test_object(a);
cout << "this also doesnt executes";
}
Make sure something (int) given to ctor has longer lifetime than an instance.
Very new to programming so forgive me for maybe not seeing something obvious.
Basically I just want to know why all three codes do compile, but the resulting executables CRASH in cases TWO and THREE
(I marked the differences with comments)
ONE - compiles
#include <iostream>
#include <string>
using namespace std;
string testrace = "dog"; //defining it only globally
class Attributes {
public:
Attributes (string race){
if (race == "human"){
intelligence = 10;}
else if (race == "dog"){
intelligence = 4;}
}
int intelligence;
};
class Dalmatian: public Attributes{
public:
// but NOT locally
Dalmatian (): Attributes{testrace} { //using it as an argument
cout << "do i even arrive here" << endl;
}
};
int main() {
Dalmatian bob;
cout << bob.intelligence << endl;
}
TWO - crashes
#include <iostream>
#include <string>
using namespace std;
class Attributes {
public:
Attributes (string race){
if (race == "human"){
intelligence = 10;}
else if (race == "dog"){
intelligence = 4;}
}
int intelligence;
};
class Dalmatian: public Attributes{
public:
string testrace = "dog"; //only defining it locally
Dalmatian (): Attributes{testrace} { //using it as argument
cout << "do i even arrive here" << endl;
}
};
int main() {
Dalmatian bob;
cout << bob.intelligence << endl;
}
THREE - crashes
#include <iostream>
#include <string>
using namespace std;
string testrace = "dog"; //defining it globally
class Attributes {
public:
Attributes (string race){
if (race == "human"){
intelligence = 10;}
else if (race == "dog"){
intelligence = 4;}
}
int intelligence;
};
class Dalmatian: public Attributes{
public:
string testrace = "dog"; // AND locally
Dalmatian (): Attributes{testrace} { //using it as argument
cout << "do i even arrive here" << endl;
}
};
int main() {
Dalmatian bob;
cout << bob.intelligence << endl;
}
What I am looking for, of course, is a working alternative to example TWO.
However I am also interested in an explanation why all of the three pieces of code will compile fine, but executables resulting from 2 and 3 will crash.
EDIT: I do know that examples ONE and THREE don't make sense, I used them for demonstrational purposes. (Also fixed my wording, compiler did fine, executable crashed);
EDIT2: I do, of course, realize that I could just replace "testrace" with ""dog"", but for easier transferability to other subclasses, I would prefer a solution that lets me use a variable argument for Attributes(), that I can vary depending on the subclass, that is invoking the main class.
First off when you have
...
string testrace = "dog";
Dalmatian (): Attributes{testrace}
...
testrace will hide the global testrace as class members supersede global variables when you are in class scope. This means both example two and three use the same variable, the class member variable.
The reason two and three crash because you are trying to use a variable that has no been constructed before. When you get to
Dalmatian (): Attributes{testrace}
testrace has not yet been constructed. Even though you have string testrace = "dog"; in the class body that initialization doesn't happen until after Attributes{testrace} is called. So Attributes (string race) gets a uninitialized string and using it is undefined behavior and also causes your crash.
The problem is that at the time that Attributes is constructed the testrace variable is yet uninitialized, and accessing it gives you undefined behavior.
You can simply fix this by writing
Dalmatian (): Attributes{"dog"} { //using it as argument
cout << "do i even arrive here" << endl;
}
See the working code here.
Before marking this as duplicate, I have already seen the other answers and they did not solve my issue.
I have two classes as follows:
A.cpp:
class A
{
public:
A();
int getValue()//just an example of a get method
{
return value;
}
private:
int value;
// a lot of variables
}
B.cpp:
class B
{
public:
B();
void addData(string fileName)
{
A* a = new A();
//reads the file with the fileName and does alot of stuff
//after calculation is over it adds the object to the vector
list.push_back(a);
}
void run()
{
thread t1(&B::simulate, list[0]);
thread t2(&B::simulate, list[1]);
t1.join();
t2.join();
}
private:
vector<A*> list;
void simulate(A* ptr)
{
int value = 0;
cout << "At first value is " << value << endl;
weight = ptr->getValue();
cout << "Then it becomes " << value << endl;
}
}
And then I have a simple main.cpp:
int main()
{
B* b = new B();
b->addData("File1.txt");
b->addData("File2.txt");
b->run();
return 0;
}
I am trying to create two threads by calling the method run(). However, when I try to compile I get the following error:
error C2672: 'std::invoke': no matching overloaded function found
I checked the other posts but nothing seemed to work for me. Any help would be appreciated.
P.S: I am using the following includes:
#include <thread>
#include <iostream>
and also:
using namespace std;
I am using other includes but they are irrelevant
B::simulate is a non-static member function so it requires 2 parameters - this and ptr, while you supplying only one. You should redeclare it as static since it does not access this class members anyway.
The code I'm working on :
I had the following code (with an error about the index in main.cpp) :
Sample.hpp :
#ifndef SAMPLE_HPP
# define SAMPLE_HPP
# include <iostream>
# include <string>
class Sample{
public:
Sample(void);
~Sample(void);
void tellname(void) const;
private:
std::string _name;
};
#endif
Sample.cpp :
#include <iostream>
#include "Sample.hpp"
Sample::Sample(void){
this->_name = "testname";
return;
};
Sample::~Sample(void){
return;
}
void Sample::tellname(void) const{
std::cout << "Name : " << this->_name << std::endl;
return;
}
main.cpp
#include "Sample.hpp"
int main(void){
int i;
Sample *test;
test = new Sample[4];
i = 0;
while (i++ < 4) // I know : i++; shouldn't be here
test[i].tellname();
delete [] test;
return 0;
}
If I compile this I get the following output :
Name : testname
Name : testname
Name : testname
Name :
My question is :
About the last line, it calls a method (void Sample::tellname(void)) but from an instance that is not in the range of the table (test[4] doesn't exist).
However, it still calls tellname() even the instance it calls it from doesn't exist. It just considers its _name field being empty.
How is this possible?
It's simply undefined behavior, something C++ imposes no requirements on so "anything could happen". What you're seeing is just a coincidence and worthless to reason about: next time you run it could crash, display nyan cat and so on.
It sounds like you are wondering why the function is called. In memory, structs do not contain the functions inside them. Instead, one copy of the functions are placed somewhere in the executable. So when you are calling test[4].tellname() what is really happening is: The address test + (4 * sizeof(Sample)) is passed to the function tellname(). The value at that address is undefined.
Here is an example to give you an idea of what is going on:
#include <iostream>
struct astruct {
int i = 0;
void prnt()
{
std::cout << i << '\n';
}
};
struct bstruct {
int y = 100;
};
int main()
{
bstruct b;
((astruct*)&b)->prnt();
getchar();
return 0;
}
Here prnt() is behind the scenes being passed the address of bstruct and treats it like the address of an astruct, since the first value in bstruct is 100, it prints 100. You can even simplify it to this:
#include <iostream>
struct astruct {
int i = 0;
void prnt()
{
std::cout << i << '\n';
}
};
int y = 100;
int main()
{
((astruct*)&y)->prnt();
getchar();
return 0;
}
i goes from 1 to 4 include, since tellname is const, test[4].tellname() is Sample::tellname with Sample being an undefined structure so "Name :" is rightfully printed then the memory in test[4]._name is printed and luckily the memory pointed by test[4]._name* is non null and is even a end string char.
So yeah you are lucky.
My problem is in the following C++ code. On the line with the 'cout' I get the error:
"'number' was not declared in this scope".
.h
using namespace std;
class a{
int number();
};
.cpp
using namespace std;
#include <iostream>
#include "header.h"
int main(){
cout << "Your number is: " << number() << endl;
return 0;
}
number(){
int x = 1;
return x;
}
Note: I'm aware this isn't the cleanest code. I just wanted to get the function working and refresh my memory on how to use headers.
For minimal fix, three basic changes are necessary.
Proper implementation of the number() method
int a::number() {
int x = 1;
return x;
}
Proper invocation of the number() method
a aObject;
cout << "Your number is: " << aObject.number() << endl;
There are many other enhancements possible though.
Addition, as pointed out by #CPlusPlus, usable scope of number() method, for example declaring it public
class a{
public:
int number();
};
Try this in your cpp file
using namespace std;
#include <iostream>
#include "header.h"
void a::number()
{
int x = 1;
return x;
}
int main()
{
cout << "Your number is: " << a().number() << endl;
return 0;
}
As for your header file replace class with a struct. The reason you are getting this error is because the compiler cant find the variable number. It is actually a method of a class.The reason you are replacing the class with a struct is because by default everything in a struct is public. So your header file called header.h should look like this
using namespace std;
struct a
{
int number();
};
There are three issues with your code.
The definition of the function number().
As you declared, it is a member function of the class "a". In your .cpp, the class name should be used as a prefix to the function. I mean,
a::number(){
int x = 1;
return x;
}
As the function is a member of the class "a", there are only two ways of accessing it,
If the function is a static function in the class, you can access it with :: operator. Like a::number().
If the function is not a static function, that is true in your case, you should instantiate the object out of the class "a" and they use "." operator with the reference. I mean,
a obj;
obj.number().
Your function number() is declared in private scope. You may recall that by default the scope is a class is private unless you specify public or protected. So the private function number() cannot be used outside the declared class unless there is a friend to it.
Below the code that I fixed,
.h
using namespace std;
class a{
public:
int number();
};
.cpp
using namespace std;
#include <iostream>
#include "header.h"
a::number(){
int x = 1;
return x;
}
int main(){
a obj;
cout << "Your number is: " << obj.number() << endl;
return 0;
}