The rule of The Big Three - class

Iam confused with the below question I did the program as per my understanding but it crashes what am I doing wrong? If someone can please assist me it would be much appreciated.
my main.cpp looks like this:
#include <iostream>
#include <iomanip>
#include "Number.h"
using namespace std;
int main()
{
Number n1(10);
Number n2 = n1;
n2.printNum();
n2.addOne();
n1 = n2;
n1.printNum();
return 0;
}
Then my header file looks like this:
#include <iostream>
using namespace std;
class Number
{
int *p;
public:
Number(int);
void addOne();
void printNum();
};
And the below parts for the constructor I need to complete there where it shows comments that's the part I should complete:
#include <iostream>
#include "Number.h"
using namespace std;
Number::Number(int a1)
{
*p = a1;//write the code needed to initialise the value of the member variable with a1
}
void Number::printNum()
{
cout << "The number is " << *p << endl;
}
void Number::addOne()
{
*p++;//write the code needed to increment the value of the member variable by one.
}
Then the question asks the below what should I do to the code to use the BIG THREE?
Consider the following program. Complete the class definition (where you are asked to) and check the output. You can see that that program works without error once it is completed. However, experts suggest that in any class that uses pointers and the new operator it is better to follow the rule of The Big Three. Modify the class definition to follow the rule of The Big Three and submit the new program and the output. Demonstrate the use of this pointer.
Thank you
Rohan

Related

pointers to vectors as class members pointers to vectors in functions

I'm a noob. Using C++ in Clion
I'm building a graph of N random nodes on a Cartesian plane
I have a simple type, node (just a point) (int x, int y)
node pt(x,y)
I have a vector of N randomly generated unique points (would this be considered ordered points btw?)
vector NodeList(N);
I have a class Graph (Incomplete) which has a function GenNodelist which I have tested as a standalone program. I had a hell of a time just getting the constructor to build without compile error.
#include <iostream>
#include <vector>
#ifndef DIJKSTRA_GRAPH_H
#define DIJKSTRA_GRAPH_H
using namespace std;
class Graph {
private:
int x;
int y;
vector<int> NodeList;
int *np;
public:
//constructor
x(x),y(y),NodeList(),np(){}
void GenNodeList(vector<int> NL(), int &np) {x,y,NodeList, &np; }
void GenNodeList(vector<int> *NL, int *p);
};
#endif //DIJKSTRA_GRAPH_H
void Graph::GenNodeList(vector<int>* NL, int* p) {
.
.
} .
... code that builds and has been quasi tested
So everything builds and there's a "hello world" main program in the project. The 2 classes, (node & Graph: 2 headers and 2 cpp files) along with the main "hello world" build and run. Now from main() I wan to call the call the GenNode function from main. I just want to pass a pointer and have the list generate and sit in memory UN-mutable. right now. I'll build the graph off of this later. When I try to call the function nothing works. How can I build this list and access it from main() and Graph()?
main(){
vector<int> NL(N);
int *np;
Graph::GenNodeList( NL, np);
}
Can't seem to figure this out.
This incomplete piece of code
Graph::GenNodeList( NL, np);
is ill-formed because that method is not static. You have to access instance of class Graph for non-static members. Nothing about oop here, just language's rules.
Graph(): x(x),y(y),NodeList(),np(){}
static void GenNodeList(vector<node>* NL, node* np);
void Graph::GenNodeList(vector<node>* NL, node* p) {
int main() {
vector<node> NL(N);
vector<node>* NList;
node *np = nullptr;
NList = &NL;
Graph::GenNodeList(NList,np);
return 0;
https://github.com/Pasqualino31/Dijkstra/tree/Pasqualino31-patch-2

How to access a variable in mumtiple cpp files contain main section in each file?

I want to access a variable in multiple .cpp files. I looked into several resources. I could not solve it though. I am using cmake to build all the codes in this project. Following is an example that exactly matches with my problem. Basically, I want val to print 42 in both code1.cpp and code2.cpp. When, I build these three files it complains: undefined reference to 'he::val' collect2: error:ld returned 1 exit status for both the .cpp files.
header1.h
#ifndef HEADER1_H
#define HEADER1_H
#include <iostream>
namespace he {
extern int val;
}
#endif // HEADER1_H
code1.cpp
#include "header1.h"
#include <iostream>
using namespace he;
int func()
{
std::cout << val << std::endl;
}
int main()
{
val=20;
func();
return 0;
}
code2.cpp
#include <iostream>
#include "header1.h"
using namespace he;
int main()
{
std::cout << val << std::endl;
}
extern int val;
Is a variable declaration. You need, somewhere (in one of your cpp files), to define the variable:
int val;
Actually, since you have two main() functions, these are two separate programs. Then your variable definition needs to be in both, like this:
namespace he {
int val;
}
But your extern declaration in the header file makes your variable global, which is usually frowned upon. It all depends, of course on what your purpose is.
Also, since two main() functions mean two separate programs, there is no variable sharing.

Mongocxx array of ObjectID in find

I'm trying to populate a query for a C++ using mongocxx driver.
The query in Javascript would be like:
{unit_id: {$in: [ObjectId('58aee90fefb6f7d46d26de72'),
ObjectId('58aee90fefb6f7d46d26de73']
}
}
I was thinking that the following code could work to generate the array part, but it doesn't compile.
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
mongocxx::instance instance {};
mongocxx::client client{mongocxx::uri{}};
mongocxx::database db = client["banff_development"];
mongocxx::collection coll = db["units"];
int main()
{
mongocxx::cursor cursor = coll.find
(document{} << "provider_id" << bsoncxx::oid("58aee90fefb6f7d46d26de4a")
<< finalize);
bsoncxx::builder::stream::document unit_filter_builder;
for (auto a: cursor)
{
unit_filter_builder << a["_id"].get_oid();
}
return 0;
}
Where can I find an working example for queries using ObjectId arrays to filter.
To build an array, you need to use the array builder, not the document builder. The declaration of the builder for the array should be bsoncxx::builder::stream::array unit_filter_builder. It also looks like you're missing a couple of includes for the various stream builder types.
As an aside, it's better to shy away from the stream builder, as it's very easy to run into issues when using it. This and this are good examples of the trickiness of using the stream builder properly. Instead of using the stream builder, you can use the basic builder, which has a much simpler implementation and gives you much saner error messages if you make a mistake.

cannot sort a vector of objects that has array in it

Hi I am using a class that has an integer and array and creating a vector of the classes objects but I cannot sort it also don't know to store in it.
I am a BEGINNER on c++ so i just wanted to know if I am wrong and how to
do that thing
here n = no of times the program has to execute
num = to store the no. of elements in vector a
but problem loop for(j=0;j<arr[i].a.end();j++)
and also pushback is not working
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class subcont
{
public:
int num;
vector<int> a;
};
int main()
{
vector<subcont> arr(100);
int i,j,k,l,n,num1,num2;
cin>>n;
for(i=0;i<n;i++)
{
cin>>arr[i].num;
for(j=0;j<arr[i].num;j++)
{
cin>>num2;
cin>>arr[i].a.pushback(num2);
}
}
for(i=0;i<n;i++)
{
sort(arr[i].a.begin(),arr[i].a.end());
}
for(i=0;i<n;i++)
{
cout<<arr[i].num;
for(j=0;j<arr[i].a.end();j++)
cout<<arr[i].a[j];
}
return 0;
}
The problems you describe sound as if you could at least compile your code, which I can't. In fact, the compiler error messages (if one first ignores the large amount of error noise generated by unhappy templates) should hint to most important problems.
Logical problems on first sight: In for(j=0;j<k;j++) the value of k is undefined. In for(j=0;j<arr[i].a.end();a++) the a++ does not make sense.
In cin>>arr[i].a.pushback[num]; the num should probably be num2. Please check your code for more such typoes.
Your sort fails because a is a C array and not a C++ container, so a.begin() and a.end() are not defined.
Stylistic problem: While it makes life a lot easier, mayn people strictly recommend to not use using namespace std;
Additional remark: Why not use std::vector<int> in place of subcont?

Why VS2015 intellisense shows error on C++11 user defined literals (UDL)

The below code can be compiled and run, but VS2015 intellisense shows error. g++ & eclipse has the same issue (compiled & run but shows error)
Does anyone know how to fix it? I tried searching on google but hopeless.
The error is a little annoying.. :-)
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::literals;
using namespace chrono_literals;
int main()
{
this_thread::sleep_for(5s);
cout << "test \n";
return 0;
}
Error message: "Invalid suffix 's' on integer literal"
Thanks a lot!
You should add some #include statements and namespace references:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(5s);
std::cout << "test \n";
return 0;
}
In your code, the compiler is not been told to use namespace std. The 5s does not work without std::literals