I'm writing a periodic table program to help me understand classes.
I want to be able to display/sort the elements by several properties such as whether it's a metal, nonmetal, or metalloid. I'm not sure hwo to do it, but my first guess was to create an array of objects; however, I'm having problems using my constructor to set the values.
Class
class Element{
public:
enum class groupNames { HYDROGEN, ALKALI, ALKALINE, GROUP_THREE, GROUP_FOUR, GROUP_FIVE,
GROUP_SIX, GROUP_SEVEN, GROUP_EIGHT, GROUP_NINE, GROUP_TEN,
GROUP_ELEVEN,GROUP_TWELVE, GROUP_THIRTEEN, GROUP_FOURTEEN,
GROUP_FIFTEEN, CHALCOGEN, HALOGEN, NOBLE_GAS
};
enum class orbitals {ORBITAL_NOTSET, S_BLOCK, P_BLOCK, D_BLOCK, F_BLOCK};
enum class metal_status {METAL = 0, METALLOID, NONMETAL};
Element();
Element(int aNumber, int pNumber,groupNames groupnames, metal_status MetalStatus, orbitals Orbital,std::string eName, std::string eSybol);
void displayProperties();
private:
groupNames groupNumber;
orbitals orbital;
metal_status metalStatus;
std::string elementSymbol;
std::string elementName;
int atomicNumber;
int periodNumber;
};
Element::Element()
{
atomicNumber = 0;
periodNumber = 0;
groupNumber = groupNames::HYDROGEN;
metalStatus = metal_status::METAL;
orbital = orbitals::ORBITAL_NOTSET;
elementName = "NULL";
elementSymbol = "NULL";
}
Element::Element(int aNumber, int pNumber,groupNames groupnames, metal_status MetalStatus, orbitals Orbital,std::string eName, std::string eSymbol)
{
groupNumber = groupnames;
metalStatus = MetalStatus;
orbital = Orbital;
atomicNumber = aNumber;
periodNumber = pNumber;
elementName = eName;
elementSymbol = eSymbol;
}
void Element::displayProperties()
{
std::cout << elementName << ", " << elementSymbol << "\n"
<< "Group Number: " << as_integer(groupNumber) << "\n"
<< "Metal Status: " << as_integer(metalStatus) << "\n"
<< "Orbital: " << as_integer(orbital) << "\n"
<< "Atomic Number: "<< atomicNumber << "\n"
<< "Period Number: "<< periodNumber;
}
Previous Method of Initialization //Works fine, the problem is I can't sort by properties
Element Hydrogen(1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Hydrogen", "H");
Element Helium(2, 1, Element::groupNames::NOBLE_GAS, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Helium", "He");
std::array Method -- Problem!
std::array<Element, 115> Elements =
{
Elements[0],
Elements[1](1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_BLOCK, "Hydrogen", "H")
};
Error: error: no match for call to '(std::array::value_type {aka Element}) (int, int, Element::groupNames,
Element::metal_status, Element::orbitals, const char [9], const char
[2])'
You may need two sets of braces (I was pulling my hair out figuring out what clang was complaining about.) I suggest uniform initialization. Also, I prefixed your enums with Element for qualification and changed them to match what their names are in your class definition.
std::array<Element, 115> Elements =
{{
{},
{1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_ORBITAL, "Hydrogen", "H"}
}};
Alternatively, you can try:
std::array<Element, 115> Elements
{
Element(),
Element(1,1, Element::groupNames::HYDROGEN, Element::metal_status::NONMETAL, Element::orbitals::S_ORBITAL, "Hydrogen", "H")
};
Related
I have two vectors:
one contains numbers and names of things;
second collects numbers that have already been showed to the user;
I'm trying to make a history list of all objects that have been shown.
Here is my code:
class palettArchive{
private:
std::vector<std::pair<int,std::string>> paletts;
int palletsCounter;
std::vector<int> choosen;
public:
//...
void history(){
auto printHist = [](int& i){
int tmp = i;
std::pair<int,std::string> tempPair = paletts[tmp];
std::cout << tempPair.first << " " << tempPair.second;
return 0;
};
std::for_each(choosen.begin(), choosen.end(), printHist);
}
};
There is an error:
error: 'this' cannot be implicitly captured in this context
std::pair<int,std::string> tempPair = paletts[tmp];
I can't make a third vector with the list that is created already. I need to make it by calling a function and printing at the time.
The lambda must capture this to be able to access member variables:
auto printHist = [this](int& i){ ... };
for_each and lambda are just making your life difficult. The simpler code is explicit iteration:
void history()
{
for (auto i : choosen) {
auto tempPair = paletts[i];
std::cout << tempPair.first << " " << tempPair.second;
// did you mean to send a newline "\n" also?
}
}
I have a class that defines object.name as "Ben" and object.favNum as 25.
I also have an array (names) to store 5 objects of this class.
Before I output the attributes of the objects in the array, I change the value of names[0].name to "Jim" and names[0].favNum to 40, using pointer notation.
Here is my code:
#include<iostream>
class Person {
public:
std::string name = "Ben";
int favNum = 25;
};
int main()
{
Person names[5];
Person *ptr = &names[0];
// Changing Values
(ptr + 0)->name = "Jim";
(ptr + 0)->favNum = 40;
for (int i = 0; i < 5; i++)
{
std::cout << (std::string) (ptr + i)->name << std::endl;
std::cout << (int) (ptr + i)->favNum << std::endl << std::endl;
}
system("pause>0");
}
Output:
Jim
40
Ben
25
Ben
25
Ben
25
Ben
25
Where I have the "Changing Values" comment, I want to replace those 2 lines with a function.
The function will be able to change the values as I am doing with the 2 lines.
This is where I am having a problem.
I'm struggling with figuring out what type to declare the function as, how to pass the pointer in, how to receive the pointer, and how to change the values of the objects in the array in the function.
I would recommend against the use of pointers, unless you really need pointers.
// use references instead
void change(Person &person) {
person.name = "Jim";
person.favNum = 40;
};
int main()
{
Person names[5];
change(names[0]);
// ...
}
The appropriate way to write such a function would be using references. A reference is similar to a pointer, but can never be nullptr. In your case you don't need nullability, you just need to edit a Person without copying it.
You probably want change() to be parametric though:
// use references instead
void change(Person &person, std::string name, int favNum) {
person.name = std::move(name);
person.favNum = favNum;
}
But now you are assigning all members of Person using this function, which makes it pointless. It would be simpler if you assigned Person. Why not use a struct and aggregate initialization:
struct Person {
std::string name = "Ben";
int favNum = 25;
};
int main()
{
Person names[5];
names[0] = {"Jim", 40};
// ...
}
On a side note, what you are doing here is unnecessary complicated:
(ptr + 0)->name = "Jim";
// this is equivalent to
ptr[0].name = "Jim";
// this is equivalent to
ptr->name = "Jim";
This is how your code should look in "real" c++ (the setting of the values will be done in the constructor function or using the setNameAndFavNum() function):
#include <array>
#include <iostream>
struct Person {
public:
Person(const std::string& name_ = "Ben", int favNum_ = 25) : name(name_), favNum(favNum_) {};
void setNameAndFavNum(const std::string& name_, int favNum_) {
name = name_;
favNum = favNum_;
};
std::string name;
int favNum;
};
int main() {
std::array<Person, 5> names;
// Changing Values
names[0].setNameAndFavNum("Jim", 40);
// Alternatively you can use the constructor and implicit copy constructor
// names[0] = {"Jim", 40};
for (int i = 0; i < names.size(); i++) {
std::cout << names[i].name << std::endl;
std::cout << names[i].favNum << std::endl << std::endl;
}
}
You shouldn't mess around with pointers and raw values when writing c++ code.
I'm working on a small custom Assembler
I have a vector of struc to storing OPCODE informations (Mnemonic, number and type of argument, parsing function,...)
typedef char args_type_t;
typedef struct op_s {
std::string opcode;
char nbr_args;
args_type_t type[4];
int code;
Tryte (*fct)(std::vector<std::string>);
} op_t;
the parsing function is pointer on a static member function :
{"MOV", 2, {T_REGISTER | T_ADDRESS, T_REGISTER | T_ADDRESS | T_CONSTANT}, 1, &Opcodes::MOV},
and the function :
class Opcodes
{
public:
static Tryte Opcodes::MOV(std::vector<std::string> _opMap) {
return Tryte(0);
}
};
I try this, but I get SEGFAULT (str() is a member function of Tryte) :
for (int i = 0; i < opMap.size(); i++) {
for (int j = 0; j < op_tab.size(); j++) {
if (!op_tab[j].opcode.compare(opMap[i][2])) {
std::cout << "OPBYTE : " << op_tab[j].fct(opMap[i]).str() << std::endl;
}
}
}
I want to call my function without instanciate Opcodes object it's posible ?
EDIT :
my error was here : if (!op_tab[j].opcode.compare(opMap[i][2]))
my mnemonic is the 1st item n opMap
Your code seems right, so perhaps a debugger information could help a bit.
But we can try to improve the friendlyness of the code by using a std::function:
typedef char args_type_t;
#include <functional>
typedef struct op_s {
std::string opcode;
char nbr_args;
args_type_t type[4];
int code;
std::function<Tryte(std::vector<std::string>>)> fct;
} op_t;
As for the sefgault, send us the backtrace.
Also, try to use range-based-for as it doesn't needs to tranverse the map to get the element again (as you are doing inside of the inner loop)
for (auto op : opMap) {
for (auto tab : op_tab) {
if (!tab.opcode.compare(op[1])) {
std::cout << "OPBYTE : " << tab.fct(op).str() << std::endl;
}
}
}
One common fix that you can do to not miss the indexes anymore is to use an Enum holding the possibilities.
enum Columns {
FuncPointer,
UserData
}
for (auto op : opMap) {
for (auto tab : op_tab) {
if (!tab.opcode.compare(op[FuncPointer])) {
std::cout << "OPBYTE : " << tab.fct(op).str() << std::endl;
}
}
}
I am trying to do string parsing (and it is proving to be a huge pain). I'm getting this error: "initialization with '{...}' expected for aggregate object "
I have an element defined like this:
Element * currentBar = new Element("Bar");
I want to make an array or something to store multiple bars, so I am trying to do something like this:
Element allBars [] = new Element("Bars");
I am pretty sure this is not what I want to do, especially since I am getting this error "initialization with '{...}' expected for aggregate object "
This is a segment of my code:
if(!chartDataString.empty()){
chartData.clear();
int index = 0;
char c, c1, c2;
inputMode currentInputMode = inputMode::UNKNOWN;
Element * cur = NULL;
while(index<chartDataString.size()){
c = chartDataString[index];
c1 = chartDataString[index+1];
c2 = chartDataString[index+2];
string s;
s = c1;
Element * currentBar = new Element("Bar");
Element allBars [] = new Element("Bars");
if(c == '*'){
if(c1 == 'i'){
currentBar->addChild(Element("rehearsalLetter", "info"));
}
else{
currentBar->addChild(Element("leftDoubleBarLine", s));
index++;
}
else if(c == '['){
currentBar->addChild(Element("leftDoubleBarLine"));
}
else if(c == 'T'){
string signature = string() + c1 + '/' + c2;
currentBar->addChild(Element("timeSignature", signature));
index += 2;
}
//start a new bar
else if(c == '|'){
allBars->addChild(currentBar);
currentBar = new Element("bar");
}
And my element class just in case it's helpful:
#include <string>
#include <ostream>
#include "ArrayList.h"
class Element{
public:
Element(){}
Element( const string& _title ){
title = _title;
}
Element( const string& _title, const string& _value){
title = _title;
value = _value;
};
~Element(){};
void addChild(Element & child){
children.add(child);
}
void serialize(ostream & o ){
if( children.size() == 0 ){
o << "<" << title << ">";
o << " " << value << " ";
o << "</" << title << ">";
}else{
o << "<" << title << ">" << endl;
for( int i = 0; i < children.size(); ++i ){
children.elementAt(i).serialize(o);
}
o << "</" << title << ">" << endl;
}
}
private:
string title;
string value;
ArrayList<Element> children;
};
When you declare a variable Element allBars [] the compiler does expect a list of values like { Element("Bar"), Element("Foo") }. This is a static array whose size is known at compile time. So for example:
Element allBars [] = { Element("Bar"), Element("Foo") };
(Beware some compiler do require the number of element to be specified in the []).
If you want to declare a dynamic array, either you use a std::vector<Element> allBars;, which I would strongly recommend, because it cause grow and shrink without you worrying for memory allocation and deallocation. Your class should have a default constructor, a copy constructor and an assignment operator (even if generated by the compiler).
Or you use a pointer that you fill with new Element[xxx] like Element* allBars = new Element[size_of_array];, that cannot grow nor shrink and that you will have to explicitly delete using delete[] in order to call the Element destructor for every element of your array.
In both case, each Elementof the array will be initialized using the default constructor.
Are you trying to initialise the array with a single Element, but want it to vary in size? If so, use a vector:
vector<Element*> allBars;
vector.push_back(new Element());
If you are able to use C++11, consider using unique_ptr to ensure no memory leaks, and use emplace_back to avoid a copy when adding to the vector:
vector<unique_ptr<Element>> allBars;
vector.emplace_back(unique_ptr<Element>(new Element()));
I have a class hierarchy as shown in the example below, where a State contains a list of ZipCodes and a list of Citys, each of which contain pointers to the ZipCodes.
The goal is to be able to update the ZipCodes without needing to update Citys (or to create new instances of City).
The C++ code below meets this requirement, but it uses pointers, which I prefer to avoid because of this and that. How can I re-design this [naive] implementation so that it doesn't rely on pointers? Thanks for any help!
EDIT: Updated code below to use boost::shared_ptr instead of raw pointers. Note that State, City, and ZipCode are just example names, and they turned out to be poor choice names (I could've picked "A", "B", and "C") because the actual code allows the equivalent of City to share ZipCodes.
#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
using namespace std;
/**
* Zone Improvement Plan (ZIP) code
*/
class ZipCode {
public:
ZipCode() : code_(0), plus4_(0) {}
ZipCode(int code, int plus4 = 0) : code_(code), plus4_(plus4) {}
virtual ~ZipCode() {};
int code() const { return code_; }
int plus4() const { return plus4_; }
void set_code(int code) { code_ = code; }
void set_plus4(int plus4) { plus4_ = plus4; }
private:
int code_;
int plus4_;
};
typedef boost::shared_ptr<ZipCode> ZipPtr;
/**
* City points to one or more zip codes
*/
class City {
public:
const vector<ZipPtr>& zip() const { return zip_; }
void add_zip_ptr(const ZipPtr x) { if (x != NULL) zip_.push_back(x); }
private:
// TODO: this vector should be a hash set
vector<ZipPtr> zip_;
};
/**
* State contains cities, each of which has pointers to
* zip codes within the state.
*/
class State {
public:
const vector<City>& city() const { return city_; }
const vector<ZipPtr>& zip() const { return zip_; }
const ZipPtr zip_of(int code) const {
for (size_t i = 0; i < zip_.size(); i++) {
if (zip_[i]->code() == code) {
return zip_[i];
}
}
return ZipPtr();
}
void add_city(const City& x) { city_.push_back(x); }
void add_zip(int code) { zip_.push_back(ZipPtr(new ZipCode(code))); }
private:
// TODO: these vectors should be hash sets
vector<City> city_;
vector<ZipPtr> zip_;
};
int main() {
State texas;
City dallas, houston;
// create state ZIPs
texas.add_zip(75380);
texas.add_zip(75381);
texas.add_zip(77219);
texas.add_zip(77220);
// point city ZIPs to the ones we just created
dallas.add_zip_ptr(texas.zip_of(75380));
dallas.add_zip_ptr(texas.zip_of(75381));
houston.add_zip_ptr(texas.zip_of(77219));
houston.add_zip_ptr(texas.zip_of(77220));
// print all ZIPs
cout << "ZIPs in Texas: " << endl;
const vector<ZipPtr>& zips = texas.zip();
for (size_t i = 0; i < zips.size(); i++) {
cout << " " << zips[i]->code() << endl;
}
cout << "ZIPs in Dallas, Texas: " << endl;
const vector<ZipPtr> zip_ptrs1 = dallas.zip();
for (size_t i = 0; i < zip_ptrs1.size(); i++) {
cout << " " << zip_ptrs1[i]->code() << endl;
}
cout << "ZIPs in Houston, Texas: " << endl;
const vector<ZipPtr> zip_ptrs2 = houston.zip();
for (size_t i = 0; i < zip_ptrs2.size(); i++) {
cout << " " << zip_ptrs2[i]->code() << endl;
}
// change a state ZIP...
cout << "Changing Houston's ZIP 77220..." << endl;
ZipPtr z = texas.zip_of(77220);
if (z != NULL) z->set_code(88888);
// ...and show the ZIPs of the affected city
cout << "ZIPs in Houston, Texas: " << endl;
const vector<ZipPtr> zip_ptrs3 = houston.zip();
for (size_t i = 0; i < zip_ptrs3.size(); i++) {
cout << " " << zip_ptrs3[i]->code() << endl;
}
return 0;
}
I see the situation as two 1:n relationships
State : City == 1 : n
City : Zipcode
== 1 : n
Based on that, I think that the State containing
vector<ZipCode> zip_;
is not sound.
I might do
class State {
vector< City > cities_in_state_;
};
class City {
vector< Zipcode > zips_in_city_;
};
This does not require pointers.
Unless you want to duplicate your ZipCode objects, you fall into this category of usage (described in your first link):
The Bar instance is actually managed
by some other part of your program,
whereas the Foo class just needs to be
able to access it.
It seems like a legit use.
However, you might want to consider the copy option (to permanently avoid problems if the vector has to reallocate its data) or make State aggregate ZipCodes from its Cities instead of distributing them ZipCodes.
The copy simply implies that you stop using pointers in City. Aggregating the ZipCodes means that instead of giving State a list of ZipCodes, you would give City the list of ZipCode instances, and when calling zip_of, you would iterate through the cities and iterate through their ZipCode collection.