i'm trying to use JsonCpp with this sample code
string json_example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
// Let's parse it
Json::Value root;
Json::Reader reader;
//Parsing of something. In example do parsing of json_example string.
bool parsedSuccess = reader.parse(json_example, root, false);
if (!parsedSuccess) {
// report to the user the failure and their locations in the document.
cout << "Failed to parse JSON" << endl << reader.getFormatedErrorMessages() << endl;
return 1;
}
// Let's extract the array that is contained inside the root object
const Json::Value array = root["array"];
// And print its values
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
// Lets extract the not array element contained in the root object and print its value
cout << "Not an array: " << root["not an array"] << endl;
// If we want to print JSON is as easy as doing:
cout << "Json Example pretty print: " << endl << root.toStyledString() << endl;
return 0;
but i've received this error on this "..array[index].asString..."
Undefined reference to `Json::Value::operator[](int) const'
can anyone help me?!tnx so much
Tnx for your answer...
So the solution is changing this
for (int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
with
for (unsigned int index = 0; index < array.size(); ++index) { // Iterates over the sequence elements.
cout << "Element " << index << " in array: " << array[index].asString() << endl;
}
bye
I think you didn't included jsoncpp.cpp file, which describes your json.h library file. U have to generate it using Pythot with command python amalgamate.py and then include it into your project
Related
So, I have a vector of boats. I need to access these boats and modify them (i.e. delete them) regularly, so it would be really nice if I could print their index along with all their other information, but I can't seem to figure out how.
The closest I got to it was with a simple for loop, but that eventually prints the current index along with the previous ones, as the vector size grows (since my i was < vector.size())
vector <Boat> berths_reg;
//print vector elements info
void Boat::print_info()
{
cout << endl;
for(int i = 0; i < berths_reg.size(); i++)
{
cout << "Index : " << i << endl;
}
cout << "Boat type : " << type << endl;
cout << "Boat length : " << length << endl;
cout << "Draft depth : " << draft << endl;
cout << endl;
}
//iterate through vector to print all elements
void print_vector()
{
vector <Boat> ::iterator it;
for (it = berths_reg.begin(); it != berths_reg.end(); ++it)
{
it->print_info();
}
}
//Storing boats (objects data) inside vector
void add_boat(Boat* b, string type, int length, int draft)
{
b->get_type(type);
b->get_length(length);
b->get_draft(draft);
berths_reg.push_back(*b);
}
Simply print both the index and the info within the same loop:
void print_vector()
{
for(int i = 0; i < berths_reg.size(); ++i)
{
cout << "Index : " << i << endl;
berths_reg[i].print_info();
}
}
So I've made a basic polynomial class in C++ which stores the coefficients of these polynomials dynamically on the heap. I'm currently in the process of overloading operators so that I can add/subtract polynomials together in order to simplify them etc.
However I'm getting unexpected results when I try to overload the * operator. It looks like instead of returning the value of an index in the array it is returning the position of the array.
This is my *operator method in my .cpp file:
Polynomial Polynomial::operator*(Polynomial p) {
int maxDegree = (degree)+(p.degree - 1);
int *intArray3 = new int[maxDegree];
int i, j;
for (int i = 0; i < degree; i++) {
for (int j = 0; j < p.degree; j++) {
cout << getCoef(i) << " * " << p.getCoef(j) << " = " << getCoef(i)*p.getCoef(j) << endl;
intArray3[j] += (getCoef(i))*(p.getCoef(j));
cout << " intArray3[" << j << "] contains : " << intArray3[j] << endl;
}
}
return Polynomial(maxDegree, intArray3);}
The lines:
cout << getCoef(i) << " * " << p.getCoef(j) << " = " << getCoef(i)*p.getCoef(j) << endl;
and
cout << " intArray3[" << j << "] contains : " << intArray3[j] << endl;
return
10 * 1 = 10
intArray3[0] contains : -842150441
in my console. I'm assuming that the problem lies with my use of pointers somewhere but I can't for the life of me think why. I implemented this overload in a similar way to my + and - overloads and they work fine. Any assistance would be greatly appreciated. Cheers.
I am trying to read pairs of data from a file using the following code.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
//**** Opening data file ****
ifstream infile;
infile.open("reg_data_log.inp");
if (infile.is_open())
{
cout << "File successfully open" << endl;
}
else
{
cout << "Error opening file";
}
//**** Reading data ***
vector<pair<double, double> > proteins;
pair<double, double> input;
while (infile >> input.first >> input.second)
{
proteins.push_back(input);
}
//**** Show data in screen ****
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
}
when compiling I have the following message
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
I cannot get my head around the problem. Can anybody help?
Look further at your loop
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
You are trying to access the property first of the integer value i. integers do not have that property it is a property of a pair object.
I think you are getting confused between iterating using iterator and indices. The simple fix is to use a range based for loop, as already suggested in comments.
for(auto d: proteins)
{
cout << d.first << ", " << d.second << endl;
}
Now what you have as is the element from the vector and not an integer. You can now access first and second.
If you cannot use range based for loop and auto, then you can use the old iterator for loop way.
for(vector<pair<double, double> >::iterator it = proteins.begin();
it != proteins.end();
++it)
{
cout << it->first << ", " << it->second << endl;
}
and someone else has already posted the how it can be done using indices, so I won't repeat that here.
As John Mopp mentions in his comment, indeed you arev referencing an int and not a pair-type. The following loop will most likely fix your issues:
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << proteins[i].first << ", " << proteins[i].second << endl;
}
I did not test this yet, but I am pretty convinced this solves your problem.
I'm having trouble getting the size of a vector by dot-walking from a parent vector into the one in question. I have verified that calling the function myfunc 1 2 3 4 5 creates five Person objects and puts them in the left vector. But when I try to get that same size to return by dot-walking from bridge to left, I get 0 as the size.
What am I doing wrong?
int main(int argc, char* argv[]) {
Person* p_ptr;
int id_source = 0;
vector<Person> left;
vector<Person> right;
bridge.push_back(left);
bridge.push_back(right);
cout << "bridge.size() = " << bridge.size() << endl;
for (int i = 1; i < argc; i++) {
id_source++;
cout << "Creating Person with crossing speed of " << argv[i] << " and id of " << id_source << endl;
p_ptr = new Person(atoi(argv[i]), id_source);
left.push_back(*p_ptr);
}
/*SIZE TESTING*/
cout << "Left side of bridge has " << left.size() << " people on it " << endl;
cout << "bridge.at(0).size() = " << bridge.at(0).size() << endl;
cout << "bridge.at(1).size() = " << bridge.at(1).size() << endl;
int slowest_id = get_slowest(0);
for (int i = 0; i < left.size(); i++) {
if (slowest_id == left.at(i).get_id()) {
p_ptr = &left.at(i);
}
}
cout << "The slowest person has id of " << slowest_id << " and speed of " << p_ptr->get_crossing_time() << endl;
}
}
left and bridge[0] are two different lists. When you call bridge.push_back(left) you make a copy of the current left list (which is empty). Elements added later will not be in the bridge version.
I have been struggling with the output of a linear search algorithm for a bit of time now. I have the function that searches the list and returns the position, either -1 if it is not found, or the numeric value of where the matching number was found. Any suggestions on how to get this to output correctly?
The output needs to search through testList, see if that number is in stdList, and give its position
Number 1 (34) was located in position 15.
Number 2 (74) was not in the file.
Number 3 (56) was not in the file.
Number 4 (103) was located in position 75.
etc.
Here is the main section of code that I am having problems with.
ARRAY_STANDARD is referring to the size of the array stdList.
stdList is the array being compared against
Position is what is being returned by the function searchList()
testList is referring to the array that is being compared to stdList
value is the element that we are searching for
//Outputs
if (position == -1)
cout << "Number " << testCount+1 << "(" << testList << ")" << " was not in the file." << endl;
else
cout << "Number " << testCount+1 << "(" << testList << ")" << " was located in position " << value << endl;
}
int searchList(int stdList [], int numElems, int value)
{
int index=0;
int position = -1;
bool found = false;
while (index < numElems && !found)
{
if (stdList[index] == value)
{
found = true;
position = index;
}
index++;
}
return position;
}
You seem to have lost a few lines of code in the last edit. What you want to do (pseudo code) is this:
for each element in testList: <<<<< this is the for statement you lost
position = findElement(element, stdList) <<<<< this is the function you were not calling
if(position < 0):
print "not found"
else:
print "found element " element " at position " position
Take it away...
You should change your method in following way:
int searchList(int stdList [], int numElems, int value)
{
int index=0;
while (index < numElems)
{
if (stdList[index] == value)
{
return index;
}
index++;
}
return -1;
}
int searchList(int stdList [], int value)
{
for(int i = 0, length = sizeof(stdList); i < length; ++i)
{
if (stdList[i] == value)
return i;
}
return -1;
}
Successful output.
int results;
for(int i = 0; i < 22; i++)
{
results = searchList(stdList, ARRAY_STANDARD, testList[i]);
if (results == -1)
cout << "Number " << i+1 << "(" << testList[i] << ")" << " was not in the file." << endl;
else
cout << "Number " << i+1 << "(" << testList[i] << ")" << " was located in position " << results+1 << endl;
}