c++ Class Members Returning Odd values - c++

I will start this out by saying c++ is my first programming language and I am a beginner at best. And I am sure this has some sort of obvious answer.
But for some reason this simple program with a single custom class is returning very odd values from the Get functions that are calling the private members of the one custom class.
The program is separated into three file as follows.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
using namespace std;
int main ()
{
double test=20.0;
Cube D(test);
cout<< D.GetSA()<<endl<<D.GetSide();
return 0;
}
then as header files for the one Classes called Shapes.h
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
using namespace std;
class Cube
{
public:
Cube();
Cube(double);
double GetSA() const;
double GetSide() const;
private:
double SA;
double V;
double Side;
};
And another file Called Shapes.cpp that contains the Constructor.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
Cube::Cube()
{
V=0.0;
SA=0.0;
Side=0.0
}
Cube::Cube(double Side2)
{
Side=Side2;
}
double Cube::GetSA() const
{
return SA;
}
double Cube::GetSide() const
{
return Side;
}
for some reason when this program is run it returns a value of 6.95293e-310 for the GetSA accessor function and returns a value of 200 for the side function.
Any ideas on why this is happening and how to fix it?

Try invoking the default constructor. It seems to initialize the data members correctly. Your parameterized constructor is only initializing the Side data member, and not any other data members.

Related

c++ unable to read memory with custom class when calling a function

I am having problems running functions within a custom class. Here is my code:
ROBOTSTRUCTURE.H
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
ROBOTSTRUCTURE.CPP
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
MAIN
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
If a set a breakpoint in main, my class is initialized correctly and everything is fine. As soon as a call the "test function" the class falls out of memory within the test function.
When it goes back to main just before return 0; the class is also out of memory. As if the pointer is deleted somehow. Can someone please explain what is happening and what did I do that is wrong?
You declared the function as bool testFunction(string input) but the function is not returning anything. This leads to undefined behavior in C++.
In addition your example wouldn't compile (you declared an argument input but you are using intput).
Your testFunction does not use any class members. If you compile this code with optimization enabled ("Release"-Configuration in VisualStudio, -O2 on gcc), the compiler might render some variables (like this) "undebuggable".

implementing map<mpz_t, double> in c++

For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>
using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;
int main()
{
map<mpz_t, double>J;
mpz_t a,b,c,n;
string tempstring;
int xrange=5,yrange=5,component=5;
mpz_set_str(n,"11", 10);
J[n]=-1;
return 0;
}
The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)
Here's the detail error page:
I don't know the details of mpz_t. However, it appears to be an array.
You can get around the problem by defining a class to be used as the key in your map.
I am able to create an executable using the following code with g++ 4.8.2.
#include <map>
using namespace std;
typedef int (mpz_t)[2];
struct MyKey
{
// Add a proper implementation of a constructor
// with mpz_t.
MyKey(mpz_t in) {}
// Add a proper implementation of copy constructor.
MyKey(MyKey const& copy) {}
// Add a proper implementation of assignment operator.
MyKey& operator=(MyKey const& rhs)
{
return *this;
}
bool operator<(MyKey const& rhs) const
{
// Add a proper implementation.
return false;
}
mpz_t n;
};
int main()
{
map<MyKey, double> J;
mpz_t n;
J[n] = 1.0;
return 0;
}

problems working with vectors to find common element

i am trying to find the common elements(song_list) between two vectors, in my user.h file i have two vectors and am trying to use this vectors in another class(help.cpp) but i cant get it right. please whats wrong with my code? it doesn't run and am not entirely sure if this is the right approach.
user.h
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
class User
{
public:
User(void);
~User(void);
public:
struct user1
{
string name;
int age;
string song_list;
};
typedef vector <user1> u1;
struct user2
{
string name;
int age;
string song_list;
};
typedef vector <user2> u2;
};
help.cpp
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements();
int commonElements(){
vector <user1> u1;
vector <user2> u2;
std::sort(u1.begin(), u1.end());
std::sort(u2.begin(), u2.end());
std::vector<string> common;
std::set_intersection(u1.begin(), u1.end(), u2.begin(), u2.end(),
std::back_inserter(common));
cout<<common<<endl;
}
thats my code so far. All data for the user's name,age and song list is on a .txt file.
You seem to be somewhat confused about types vs. instances. You're creating some separate types where (I'm pretty sure) you just want two instances. For example, it appears (to me) that you really want u1 and u2 to be vectors of the same type of objects.
user.h:
#pragma once
#include <string>
#include <vector>
#include <string>
using namespace std;
struct user {
string name;
int age;
vector<string> song_list;
};
help.cpp:
#include "Help.h"
#include "User.h"
#include <iterator>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
int commonElements(user &u1, user &u2) {
sort(u1.song_list.begin(), u1.song_list.end());
sort(u2.song_list.begin(), u2.song_list.end());
vector<string> common;
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
back_inserter(common));
for (auto const &song : common)
cout << song << "\n";
#if 0
// alternatively, write the intersection directly to the stream:
set_intersection(u1.song_list.begin(), u1.song_list.end(),
u2.song_list.begin(), u2.song_list.end(),
ostream_iterator<string>(cout, "\n"));
#endif
}
As an aside, it's generally agreed that putting a using namespace std; in a header is a really lousy idea. I've left it because it's mostly unrelated to the question(s) at hand, but it should really be changed.

using vector in c++ class definition

I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.
main.cpp
#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
//vector declaration works here
return 0;
}
tour.h
#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
public:
//variables
int teamID;
vector<int> tourseq; //this is where the error occurs
//functions
tour(int);
};
#endif /* TOUR_H_ */
tour.cpp
#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}
What could be wrong here?
Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.
You probably shouldn't put using namespace std; in your code anyway.

Using class instead of struct and constructor issues

I am trying to move from using structs to using classes, and I have a few questions with my - not fully complete, but enough to illustrate my queries - code (thank you in advance for clarifying these):
I am having problems with creating a constructor that takes in arguments, specifically the line in the header file that I have currently left as neighborAtt(int neighbor_id, int att_1, int att_2);.
When using neighborAtt as a struct, I could do it easily as neighborAttributes currentNode(neighborID, att1, att2);. What is the class-equivalent?
In the .cpp file, I know that I need to define the constructor as neighborAtt::neighborAtt().
Do I need to this with the functions (i.e. include neighborAtt::) or is what I've done accurate?
This is my header file:
#if !def connectivity_H
#define connectivity_H
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <sstream>
#include <fstream>
class listAtt;
class vecListAtt;
class neighborAtt //contains the neighbour and associated attributes of a node
{
public:
neighborAtt();
neighborAtt(int neighbor_id, int att_1, int att_2);
vecListAtt connFrFile(int file_ext);
vecListAtt makeList(std::vector<std::list<neighborAtt>> nodeAndInfo, int nodeID, neighborAtt neighAndAtt);
neighborAtt getAtt(std::string currentLine);
private:
int neighborID;
int attribute1;
int attribute2;
};
typedef std::list<neighborAtt> listAtt;
typedef std::vector<listAtt> vecListAtt;
#endif
and the .cpp file:
#include "stdafx.h"
#include "connectivity.h"
neighborAtt::neighborAtt(): neighborID(0), attribute1(0), attribute2(0) {}
//neighborAtt::neighborAtt constructor with arguments
vecListAtt connFrFile(int file_ext)
{
//code
}
neighborAtt getAtt(std::string line)
{
//code
}
For the second constructor (one with the arguments) you do just the same as for one without them. Or did I get the question wrong? It'd be like:
neighborAtt::neighborAtt(int neighbor_id, int att_1, int att_2)
: neighborID(neighbor_id),
attribute1(att_1),
attribute2(att_2)
{
}
And for the methods you must go the same way:
vecListAtt neighborAtt::connFrFile(int file_ext)
{
//code
}