I tried using this code sf::Vector2f position = game.player_position(); std::cout << "position : " << position << std::endl; but it didn't work. Here the code in the "player_position()" function
return sprite.getPosition();
i tried to print player position but it didn't work it gives me an error, the error says "no operator "<<" matches these operands -- operand types are: std::basic_ostream<char, std::char_traits> << sf::Vector2f"
You are trying to print a vector and not the values to the standard output. This is not defined. As an easy solution, you can print the x and y values separately.
#include <SFML/Graphics.hpp>
#include <iostream>
int main (int argc, const char * argv[]){
sf::Vector2f v1(16.5f, 24.f);
std::cout << v1.x << ", " << v1.y << '\n';
return 1;
}
Output is 16.5, 24
Related
I'm currently using visual studio 2022
I'm using vcpkg pcl:x64 library install.
pcl version: 1.9.1-12
I'm expecting to be able to access 3 vertices per polygon.
Unfortunately, I can't seem to access the vertices associated with each triangle.
#include <Eigen/Dense>
#include <pcl/common/io.h>
#include <pcl/filters/extract_indices.h>
#include <pcl/filters/passthrough.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/PolygonMesh.h>
#include <pcl/TextureMesh.h>
int main
{
pcl::PolygonMesh mesh;
pcl::io::loadPolygonFileOBJ("pathtomesh.meshfile.obj", mesh);
pcl::PointXYZ v = mesh.polygons[0].vertices[0];
}
the error i recieve is:
no suitable constructor exists to convert from "boost::random::seed_seq::result_type" to "pcl::PointXYZ"
It seems as though vertices is an unsigned int and not a pcl::PointXYZ. This is kinda weird to me because I was expecting a double or a floating point to store the vertices coordinates. It turns out that mesh.polygons[0].vertices[0] returns the indices of each point in the mesh that are stored in the point cloud. So i was able to find the points that the function mesh.polygons[0].vertices[0] were pointing at by using converting the mesh to a pcl::PointCloudpcl::PointXYZ and putting the indexes into that function.
pcl::PolygonMesh mesh;
pcl::io::loadPolygonFileOBJ("D:\\testOBJs\\cube.obj", mesh);
pcl::PointCloud<pcl::PointXYZ>::Ptr allVertices(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromPCLPointCloud2(mesh.cloud, *allVertices);
std::cout << "All Vertices" << std::endl;
for (int i = 0; i < allVertices->size(); i++)
{
std::cout << std::to_string(i) + " " << allVertices->points[i] << std::endl;
}
std::cout << "All Polygons" << std::endl;
for (int i = 0; i < mesh.polygons.size(); i++)
{
std::cout << std::endl;
std::cout << mesh.polygons[i].vertices[0] << std::endl;
std::cout << mesh.polygons[i].vertices[1] << std::endl;
std::cout << mesh.polygons[i].vertices[2] << std::endl;
std::cout << std::endl;
}
I want to read a chunk of data from file into stringstream, which later will be used to parse the data (using getline, >>, etc). After reading the bytes, I set the buffer of the stringstream, but I cant make it to set the p pointer.
I tested the code on some online services, such as onlinegdb.com and cppreference.com and it works. However, on microsoft, I get an error - the pointers get out of order.
Here's the code, I replaced the file-read with a char array.
#include <sstream>
#include <iostream>
int main()
{
char* a = new char [30];
for (int i=0;i<30;i++)
a[i]='-';
std::stringstream os;
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
os.rdbuf()->pubsetbuf(a,30);
os.seekp(7);
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
}
the output I get when it works
g 0 p 0
g 0 p 7
the output I get on visual studio 2015
g 0 p 0
g -1 p -1
any ides?
thanks
std::sstream::setbuf may do nothing:
If s is a null pointer and n is zero, this function has no effect.
Otherwise, the effect is implementation-defined: some implementations do nothing, while some implementations clear the std::string member currently used as the buffer and begin using the user-supplied character array of size n, whose first element is pointed to by s, as the buffer and the input/output character sequence.
You are better off using the std::stringstream constructor to set the data or call str():
#include <sstream>
#include <iostream>
int main()
{
std::string str( 30, '-' );
std::stringstream os;
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
os.str( str );
os.seekp(7);
std::cout << "g " << os.tellg() << " p " << os.tellp() << std::endl;
}
I am trying to load the pixel rgb/ga information of a png image into a matrix, using the library png++, to do some computations with them.
My Code (which does not work at the moment):
#include <iostream>
#include <png++/image.hpp>
#include <png++/rgb_pixel.hpp>
int main(int argc, const char * argv[]) {
const std::string path="img_03.png";
png::image< png::basic_rgb_pixel <unsigned char> > pic(path);
pixel=pic.get_pixel(0, 0);
pixelp = &pixel;
std::cout << "value=" << pic[10][10].red << std::endl; //output: '?'
std::cout << "value=" << pixel.red << std::endl; //nothing
std::cout << "pointer=" << pixelp << std::endl; //delivers adress
pic.read(path);
std::cout << "value=" << pic[10][10].red << std::endl; //nothing
pic.write("picOutput.png"); //same picture
return 0;
}
However, none of those methods work to get the rgb values of each pixel.
If there is another way to get rgb/ga information of each pixel, please mention it.
The line pic.write("picOutput.png"); delivers the same png i loaded in the line pic.read(path). This is a personal exercise for me to get more used to C++, criticise my code as much as you can.
Thanks!
Here comes the solution:
change line:
std::cout << "value=" << pic[10][10].red << std::endl; //nothing
with:
std::cout << "value=" << (int) pic[10][10].red << std::endl; //nothing
because std::cout can't output types of unsigned char.
Thanks to Alex!
For in-depth explanation, look here:
cout not printing unsigned char
I was just reviewing my C++. I tried to do this:
#include <iostream>
using std::cout;
using std::endl;
void printStuff(int x);
int main() {
printStuff(10);
return 0;
}
void printStuff(int x) {
cout << "My favorite number is " + x << endl;
}
The problem happens in the printStuff function. When I run it, the first 10 characters from "My favorite number is ", is omitted from the output. The output is "e number is ". The number does not even show up.
The way to fix this is to do
void printStuff(int x) {
cout << "My favorite number is " << x << endl;
}
I am wondering what the computer/compiler is doing behind the scenes.
The + overloaded operator in this case is not concatenating any string since x is an integer. The output is moved by rvalue times in this case. So the first 10 characters are not printed. Check this reference.
if you will write
cout << "My favorite number is " + std::to_string(x) << endl;
it will work
It's simple pointer arithmetic. The string literal is an array or chars and will be presented as a pointer. You add 10 to the pointer telling you want to output starting from the 11th character.
There is no + operator that would convert a number into a string and concatenate it to a char array.
adding or incrementing a string doesn't increment the value it contains but it's address:
it's not problem of msvc 2015 or cout but instead it's moving in memory back/forward:
to prove to you that cout is innocent:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char* str = "My favorite number is ";
int a = 10;
for(int i(0); i < strlen(str); i++)
std::cout << str + i << std::endl;
char* ptrTxt = "Hello";
while(strlen(ptrTxt++))
std::cout << ptrTxt << std::endl;
// proving that cout is innocent:
char* str2 = str + 10; // copying from element 10 to the end of str to stre. like strncpy()
std::cout << str2 << std::endl; // cout prints what is exactly in str2
return 0;
}
There are 2 programs below. In the first I have striped out everything but the problem loop and when I do that it works. In the second program (still striped down from the one I am working on) I use the same logic but the sub string called for_loop_buffer never loads.
Basically I am parsing 1 string which holds a record from a cdf file. The for loop buffer is where I build each field and save it to my class. I have been spinning my wheels on this for days any help would be appreciated.
EDIT:
Here are the contents from the file the program is reading:0,Nicole 0,Debbie –
Program 1
/*******************************************************\
* Debug Version 02-b-1 - string to string build *
* *
* Saving a Person Object to file *
* This program will have a class that can save and *
* retrieve itself to and from a flat file in cdf format *
* *
* The program will accept screen input for subsequent *
* Object store call and will allow a print command that *
* will create a file of mailing labels. *
\*******************************************************/
#include <cstdlib>
#include <iostream>
#include <string>
std::string current_person = "Why does this not work? "; // CDF String version of person record from file
std::string for_loop_buffer = " "; // the substring buffer
int counter2 = 0;
int main( )
{
for (int i = 0;i <= 23;++i){
/*****************************************************************************\
* This next line apears to be the problem *
\*****************************************************************************/
for_loop_buffer[counter2] = current_person[i];
std::cout << "DEBUG - Current person " << current_person << std::endl;
std::cout << "DEBUG - current Person element " << current_person[i] << std::endl;
std::cout << "DEBUG - for Loop Buffer " << for_loop_buffer << std::endl;
std::cout << "DEBUG - for Loop Buffer element " << for_loop_buffer[counter2] << std::endl;
std::cout << "DEBUG - for Loop Buffer counter " << counter2 << std::endl;
++counter2;
} // close for
return (0);
}
Program 2
/*******************************************************\
* Debug Version 02 - Read data from a cdf file *
* *
* Saving a Person Object to file *
* This program will have a class that can save and *
* retrieve itself to and from a flat file in cdf format *
* *
* The program will accept screen input for subsequent *
* Object store call and will allow a print command that *
* will create a file of mailing labels. *
\*******************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/***************************************************************\
* Class Definition - person *
* *
* Member functions *
* save_person -- Save the person object to a flat file in CDF *
* get_person -- Retrives a persons data from a CDF flat file *
\***************************************************************/
// Definition of the Class
class person_class {
public:
struct person_contact {
int person_number; // System assigned not on ui
std::string first_name;
};
// Declarations for the method Prototypes
public:
// A Function that retrieves 1 person from file and
// provides it to the user
void get_person();
} person;
/*********************************************************\
* person_class::get_person -- get a data record from file *
* *
\*********************************************************/
inline void person_class::get_person()
{
// Declaration of private variables for this method
std::string ui_first_name; // User Input from Keyboard
person_contact target_person; // Name of the opbect we are filling
std::string current_person; // CDF String version of person record from file
std::cout << "Enter the First Name of the person you are looking for: ";
std::cin >> ui_first_name;
std::cout << std::endl << "Matching Records: " << std::endl;
// Open file
std::ifstream input_file("ntc_db02test.ntc");
std::cout << "DEBUG - I opened the file " << std::endl;
current_person.clear();
while (input_file.good()){
std::cout << "DEBUG - I have entered the while loop " << std::endl;
// Grab the next record from a comma delimited file
getline(input_file,current_person);
std::cout << "DEBUG - I have performed a getline " << std::endl;
std::cout << current_person << std::endl;
// I need a mechanism to build substrings
// Substring Buffer stuff
int buffer_pointer = 0; // pointer to the next position in the substring buffer
std::string for_loop_buffer =" "; // the substring buffer
for_loop_buffer.clear();
int field_index = 0; // Which field is next to load
/*******************************************************************\
* If everything went as planned I have a single CDF person record *
* in the current_person string and now all I have to do is parse *
* it and place each field in the current instance. *
\*******************************************************************/
// load the instance with the record data from the file
for (int i = 0;i < current_person.length();++i){
std::cout << "DEBUG - I am in the for loop " << std::endl;
// look for the end of the CDF field
if(current_person[i] == ','){
// Write the buffer to the next field
std::cout << "DEBUG - I found a comma " << std::endl;
switch (field_index) {
case 0:
stringstream(for_loop_buffer) >> target_person.person_number;
std::cout << "DEBUG - Field Index " << field_index << std::endl;
std::cout << "DEBUG - For Loop Buffer " << for_loop_buffer << std::endl;
std::cout << "DEBUG - Person Number " << target_person.person_number << std::endl;
break;
case 1:
stringstream(for_loop_buffer) >> target_person.first_name;
std::cout << "DEBUG - First Name " << target_person.first_name << std::endl;
break;
default:
std::cout << "This should not happen, index not functioning " << '\n';
break;
} // Close Switch
// clear the buffer
for_loop_buffer.clear(); //SE
buffer_pointer = 0;
// set up the next field load - increment the field index
++field_index; //
}else{ // close if
std::cout << "DEBUG - not a comma " << std::endl;
// If the character is not a comma
// add the character to the buffer
/*****************************************************************************\
* !!!!!!!!!!!! This next line apears to be the problem !!!!!!!!!!!!!!! *
\*****************************************************************************/
for_loop_buffer[buffer_pointer] = current_person[i];
std::cout << "DEBUG - Primary Index i " << i << std::endl;
std::cout << "DEBUG - current Person element " << current_person[i] << std::endl;
std::cout << "DEBUG - Buffer Pointer " << buffer_pointer << std::endl;
std::cout << "DEBUG - for Loop Buffer element " << for_loop_buffer[buffer_pointer] << std::endl;
std::cout << "DEBUG - for Loop Buffer " << for_loop_buffer << std::endl;
// Increment the buffer pointer
++buffer_pointer;
} // close else
} // close for
if (target_person.first_name == ui_first_name){
// Code to print out full record
std::cout << target_person.first_name << " "
<< std::endl;
} // Close if
} // Close While
input_file.close();
} // Close Class
/******************************************************************************\
* The idea this time is to do as little in the main as possible and as much *
* in the class as is appropriate. So rather than read in data and pass it to *
* the method, I will call the method and let it do its own UI reading. *
\******************************************************************************/
int main( )
{
/**********************************************************\
* Ask the user if they want to Create a new person record *
* n or find a record in the database f *
\**********************************************************/
// Set up for user input
char command = 'f';
std::cout << " What do you want to do? New Person (n) or Find Person (f) ";
std::cin >> command;
while (command == 'n' || command == 'f'){
if (command == 'n'){
std::cout << "Do Nothing" << std::endl;
} else if (command == 'f'){
person_class current_person_object;
current_person_object.get_person();
}
command = 'x';
std::cout << "I am Back in Main" << std::endl;
}
std::cout << "You entered something other than a -n- or a -f-, program terminating now" << std::endl;
return (0);
}
You are looping through the string, splitting it up on commas, however there are standard library functions that can simplify this for you. This sounds like homework, so I wont post any code answers, but here are some hints:
std::string::find_first_of will search through a string for you, and tell you where the next comma is.
std::string::substr given a position in the string and a length, will return a part of the string.
Using the above two functions in a loop you could pick out your fields in less code, that's easier to follow.
Alternatively, it would be possible to read the values straight from the stream, as the std::getline function you are using has an extra parameter to specify what character terminates the line (i.e. the comma).
Hard to see through the code, but in your "real" code you say for_loop_buffer.clear();, which is synonymous to for_loop_buffer = "";, and later I think you are accessing a member of the (now empty) string:
for_loop_buffer[buffer_pointer] = current_person[i]; // Illegal access!