' was not declared in this scope' C++ - c++

I have some rude errors... I search the web about it, and i was able to read her everywhere, but still not fix my problem ...
Here is my main :
#include <iostream>
#include "SFML/Network.hpp"
#include "decode.hpp"
#include "listOfFunction.hpp"
#include "map.hpp"
void createSocket(){
unsigned short bindPort = 12800;
unsigned short clientPort;
int ret = 0;
sf::UdpSocket socket;
sf::IpAddress clientAddr;
sf::Packet packet;
Map mainMap;
if (socket.bind(bindPort) != sf::Socket::Done){
if (sf::Socket::Error) std::cout << "An unexpected error happened : Fatal Error !" << std::endl;
if (sf::Socket::NotReady) std::cout << "The socket is not ready to send/receive data yet !" << std::endl;
}
while(1){
packet.clear();
socket.receive(packet, clientAddr, clientPort);
std::string header = readFromPacket(packet);
ret = callFunction(packet, header, clientAddr, clientPort, mainMap, socket);
}
}
int main(){
createSocket();
}
Here are the errors :
error : 'Map' was not declared in this scope
error : expected ';'before 'mainMap'
error : 'mainMap' was not declared in this scope
error : callFunction was not declared in this scope
Map.cpp :
#include <iostream>
#include <vector>
#include "SFML/Network.hpp"
#include "map.hpp"
Map::Map(){
char number[5] = "\0";
int m_obstacle[80000] = {0};
std::string m_error = "not error";
std::vector <int> m_posPlayer(0);
std::vector <std::string> m_namePlayer(0);
std::vector <sf::IpAddress> m_addressPlayer(0);
std::string m_stringObstacle = "";
for (int i=0;i<80000;i++){
sprintf(number, "%d", m_obstacle[i]);
m_stringObstacle += std::string(number) + "+";
}
}
int Map::sendMap(sf::IpAddress address, unsigned short clientPort, sf::UdpSocket& socket){
std::string header = "rcv_map";
sf::Packet packet;
packet >> header >> m_stringObstacle;
if(socket.send(packet, address, clientPort)!=sf::UdpSocket::Done){
m_error += "Error sending the packet \n";
}
return 0;
}
int Map::error(){
if (m_error != "not_error"){
std::cout << "here is a following list of errors : " << m_error << std::endl;
}
m_error.erase(0,m_error.length());
}
Map.hpp:
#include <iostream>
#include "SFML/Network.hpp"
#ifndef DECODE_H_INCLUDED
#define DECODE_H_INCLUDED
class Map{
public:
Map();
int sendMap(sf::IpAddress address, unsigned short clientPort, sf::UdpSocket& socket);
int error();
private:
int m_obstacle;
std::vector <int> m_posPlayer;
std::vector <int> m_namePlayer;
std::vector <sf::IpAddress> m_addressPlayer;
std::string m_stringObstacle;
};
#endif // DECODE_H_INCLUDED
The problem probably exists in the headers, but I can't figure it out.

Problems are probably about headers, but can't find why.
That is absolutely right! You have applied inclusion guards incorrectly.
You have used the inclusion guard from a wrong hpp file:
#ifndef DECODE_H_INCLUDED
in map.hpp comes from decode.hpp. It should be
#ifndef MAP_H_INCLUDED

I think that the problem is related to these macro definitions in Map.hpp
#ifndef DECODE_H_INCLUDED
#define DECODE_H_INCLUDED
It seems they are the same definitions that are used in decode.hpp that is included before Map.hpp
Take into account that at least this constructor is wrong does not make sense and will not compile
Map::Map(){
int i = 0;
int m_obstacle[80000] = {0};
std::vector <int> m_posPlayer(0);
std::vector <std::string> m_namePlayer(0);
std::vector <sf::IpAddress> m_addressPlayer(0);
std::string m_stringObstacle = "";
for (i=0;i<80000;i++){
m_stringObstacle += m_obstacle[i] + "+";
}
}
These local objects
std::vector <int> m_posPlayer(0);
std::vector <std::string> m_namePlayer(0);
std::vector <sf::IpAddress> m_addressPlayer(0);
std::string m_stringObstacle = "";
are not used.
I think you mean class data members instead of the local objects.:)
And this statement
m_stringObstacle += m_obstacle[i] + "+";
is wrong and does not make sense.
Also data member
int m_obstacle;
is declared like a scalar object. You may not redeclare it in the constructor like an array.

Related

How to Define CountProbation() function using Functions from other Files?

I have been racking my brain on how to properly define the function CountProbation() properly in the Course.css file. I know that a for and if loop should probably be included but I am having trouble including functions from other files, even though I am including a header tag at the beginning of the current Course.css file.
Below are the C++ files that are given in the lab:
NOTE: Only the Course.cpp file is the one that needs to be edited. All of the other files are READ ONLY and purely for your information as the reader.
Sorry if it seems like a lot of code, but I didn't just want to include the Course.cpp file because then you might not understand the program.
Here are the compiler Errors/Warnings:
Course.cpp: In member function ‘int Course::CountProbation()’:
Course.cpp:8:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<Student>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
8 | for (int i=0; i < roster.size(); ++i) {
| ~~^~~~~~~~~~~~~~~
Course.cpp:9:9: error: ‘GetGPA’ was not declared in this scope
9 | if (GetGPA() < 2.0) {
| ^~~~~~
Course.cpp
#include <iostream>
#include "Course.h"
#include "Student.h"
using namespace std;
int Course::CountProbation() { //This is the function that I have tried to define as you can see by my
int probCount; //code
for (int i=1; i < roster.size(); ++i) {
if (GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
return probCount;
}
void Course::AddStudent(Student s) {
roster.push_back(s);
}
Course.h (READ ONLY)
#ifndef COURSE_H
#define COURSE_H
#include <vector>
#include "Student.h"
class Course {
public:
int CountProbation();
void AddStudent(Student s);
private:
vector<Student> roster; //collection of Student objects
};
#endif
Main.cpp (READ ONLY)
#include <iostream>
#include <string>
#include "Course.h"
using namespace std;
int main() {
Course course;
int probCount;
// Example students for testing
course.AddStudent(Student("Henry", "Cabot", 3.2));
course.AddStudent(Student("Brenda", "Stern", 1.1));
course.AddStudent(Student("Lynda", "Robison", 2.4));
course.AddStudent(Student("Jane", "Flynn", 1.8));
probCount = course.CountProbation();
cout << "Probation count: " << probCount << endl;
return 0;
}
Student.h (READ ONLY)
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
// Class representing a student
class Student {
public:
Student(string first, string last, double gpa);
double GetGPA() ;
string GetLast();
private:
string first; // first name
string last; // last name
double gpa; // grade point average
};
#endif
Student.cpp (READ ONLY)
#include "Student.h"
// Student class constructor
Student::Student(string first, string last, double gpa) {
this->first = first; // first name
this->last = last; // last name
this->gpa = gpa; // grade point average
}
double Student::GetGPA() {
return gpa;
}
string Student::GetLast() {
return last;
}
This loop (as it was coded in the original question, before it was edited):
for (int i=1; i < roster.size(); ++i) {
if (Student.GetGPA() < 2.0) {
probCount = probCount + 1;
}
}
is flawed for three reasons:
i should start from 0, rather than 1
i should be of type size_t, rather than int
Student is a type, not a variable
A nice way to solve all these problems is to use a ranged for loop, like this:
for (Student &student : roster)
{
if (student.GetGPA() < 2.0)
probCount = probCount + 1;
}
As mentioned in the comments, you also need to initialise probCount to zero before entering the loop:
int probCount = 0;
Finally, it's worth noting that the above loop would normally be coded as:
for (const Student &student : roster)
{
...
but that won't work here as GetGPA is not declared as a const method. Thanks to #user4581301 for pointing this out.

C++ Dynamic Array: A value of type "void" cannot be used to initialize an entity of type "int"

I am working on a C++ project for school in which the program will read in a list of numbers from a text file, store them in a dynamic array, then print them out to another text file. To be honest I'm a little lost with the pointers in this, and I am getting the error "A value of type "void" cannot be used to initialize an entity of type "int"" in my main source file.
Main.cpp (this is where I'm getting the error):
#include "dynamic.h"
int main
{
readDynamicData("input.txt","output.txt");
}
dynamic.cpp (the skeleton for the program):
#include "dynamic.h"
void readDynamicData(string input, string output)
{
DynamicArray da; //struct in the header file
da.count = 0;
da.size = 5; //initial array size of 5
int *temp = da.theArray;
da.theArray = new int[da.size];
ifstream in(input);
ofstream out(output);
in >> da.number; //prime read
while (!in.fail())
{
if (da.count < da.size)
{
da.theArray[da.count] = da.number;
da.count++;
in >> da.number; //reprime
}
else grow; //if there are more numbers than the array size, grow the array
}
out << "Size: " << da.size << endl;
out << "Count: " << da.count << endl;
out << "Data:" << endl;
for (int i = 0; i < da.size; i++)
out << da.theArray[i];
in.close();
out.close();
delete[] temp;
}
void grow(DynamicArray &da) //this portion was given to us
{
int *temp = da.theArray;
da.theArray = new int[da.size * 2];
for (int i = 0; i<da.size; i++)
da.theArray[i] = temp[i];
delete[] temp;
da.size = da.size * 2;
}
and dynamic.h, the header file:
#include <iostream>
#include <string>
#include <fstream>
#ifndef _DynamicArray_
#define _DynamicArray_
using namespace std;
void readDynamicData(string input, string output);
struct DynamicArray
{
int *theArray;
int count;
int size;
int number;
};
void grow(DynamicArray &da);
#endif
you have to add parenthesis to main or any function:
int main(){/*your code here ...*/};
2- you are using an unitialized objct:
DynamicArray da; //struct in the header file
da.count = 0;
da.size = 5; //initial array size of 5
so int* theArray is a member data and is uninitialized so welcome to a segfault
all the members of da are not initialized so you have to do before using it.
3- also you add parenthesis to grow function:
else grow(/*some parameter here*/); // grow is a function
4- using namespace std; in a header file is a very bad practice.
tip use it inside source
5- why making inclusion of iostream and string.. before the inclusion guard??
correct it to:
#ifndef _DynamicArray_
#define _DynamicArray_
#include <iostream>
#include <string>
#include <fstream>
/*your code here*/
#endif
main is a function so it needs brackets:
int main(){
// your code
return 0; // because it should return intiger
}
And. Your grow is also a function, so if you want to call it you write grow() and it needs DynamicArray as a parameter.
It is impossible to write working programs on C/C++ any programming language not knowing a basic syntax.

Undefined reference errors c++

I'm having trouble linking my program. All the classes compile fine with
g++ -c main.cpp
g++ -c Server.cpp
g++ -c Client.cpp
But when I go to link them
g++ main.o Server.o Client.o -o main.out -lsfml-network -lsfml-system
I'm getting undefined references for the functions in my Client and Server classes.
main.cpp:(.text+0x1ba): undefined reference to `(anonymous namespace)::Server::Server()'
main.cpp:(.text+0x1c6): undefined reference to `(anonymous namespace)::Server::getMessage()'
main.cpp:(.text+0x210): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x227): undefined reference to `(anonymous namespace)::Client::Client()'
main.cpp:(.text+0x23d): undefined reference to `(anonymous namespace)::Client::sendMessage(std::string const&)'
main.cpp:(.text+0x287): undefined reference to `(anonymous namespace)::Client::~Client()'
main.cpp:(.text+0x3e9): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x407): undefined reference to `(anonymous namespace)::Client::~Client()'
collect2: error: ld returned 1 exit status
Any help is much appreciated.
main.cpp
#include <iostream>
#include <cstdio>
#include <string>
#include "include/Server.hpp"
#include "include/Client.hpp"
void printOptions(std::string const* const, size_t const&);
char const getInput(std::string const* options, size_t const& size);
int main()
{
const std::string YES_NO[] = {
"Yes",
"No"
};
const std::string OPTIONS[] = {
"Server",
"Client"
};
const std::string CONTINUE = "Continue?\n\n";
const std::string PROMPT = "Run as?\n";
const std::string RECIPEINT = "127.0.0.1";
const size_t SIZE = sizeof(OPTIONS) / sizeof(std::string);
std::cout << PROMPT;
const char INPUT = getInput(OPTIONS, SIZE);
char response;
// Server
if (INPUT == '1')
{
Server server;
do
{
server.getMessage();
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// Client
else if (INPUT == '2')
{
Client client;
do
{
client.sendMessage(RECIPEINT);
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// else serious problem
}
/* Function used to display a list of options to the user.
Each option is displayed on a new line preceeded with
its input number and provided option text.
Ex:
options[] = {"Option A", "Option B"}
Will print:
1) Option A
2) Option B
#param options
An array of std::string that will be displayed
as the list of options.
#param size
The number of different options the options array contains.
*/
void printOptions(std::string const* options, size_t const& size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << i + 1 << ") ";
std::cout << options[i] << std::endl;
}
}
/* Used to return a users choice from a list of options.
*WARNING*
Providing an array with more than 9 options will give
unexpected return values.
If it is necesszary to provide a user with mroe than 9 options
have the 9th option be "More options..." from which you may call
getInput() again, with additonal options.
#param options
An array of std::string that will be displayed
for the users choice.
#param size
The number of different optins the optinos array contains.
#return const char
A number from 1 to 9 representing the users choice from
the options array.
1 = array index 0, 9 = array index 8.
*/
const char getInput(std::string const* options, size_t const& size)
{
printOptions(options, size);
char input;
bool needInput = true;
while (needInput)
{
std::cout << "Enter a number: ";
std::cin >> input;
for (size_t i = 0; i < size; i++)
{
char optionBuffer[2];
std::sprintf(optionBuffer, "%zu", i + 1);
if (input == *optionBuffer)
{
needInput = false;
break;
}
}
if (needInput)
std::cout << "Option not available." << std::endl;
}
return input;
}
Server.hpp
#pragma once
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
namespace
{
class Server
{
public:
Server();
~Server();
static const unsigned short SERVER_PORT = 54000;
void getMessage();
protected:
private:
sf::UdpSocket socket;
};
};
Server.cpp
#include "include/Server.hpp"
//using namespace;
Server::Server()
{
this->socket.bind(SERVER_PORT);
}
Server::~Server() {}
// TODO: Ensure that the socket is bound to a port.
void Server::getMessage()
{
sf::IpAddress sendersAddress;
unsigned short sendersPort;
sf::Packet sendersPacket;
// Failed to recieve packet
if (this->socket.receive(sendersPacket, sendersAddress, sendersPort)
!= sf::Socket::Done
)
{
std::cout << "Failed to recieve packet." << std::endl;
}
// Sucessfully recievd packet
else
{
std::string message;
sendersPacket >> message;
std::cout << "Recieved message:\n\n" << message << std::endl;
}
}
Client.hpp
#pragma once
#include "Server.hpp"
#include <SFML/Network.hpp>
namespace
{
class Client
{
public:
Client();
~Client();
void sendMessage(std::string const&);
protected:
private:
sf::UdpSocket socket;
};
};
Client.cpp
#include "include/Client.hpp"
//using namespace;
Client::Client()
{
this->socket.bind(sf::Socket::AnyPort);
}
Client::~Client()
{
}
void Client::sendMessage(std::string const& recipient)
{
std::string message;
sf::Packet packet;
std::cout << "Write a message:\n" << std::endl;
do
{
std::getline(std::cin, message);
} while (!message.size());
socket.send(packet, recipient, Server::SERVER_PORT);
}
The problem is that your Server and Client class declarations are inside unnamed (or anonymous) namespaces. To fix it, simply give your namespaces a name, such as:
namespace MyNamespace
{
class Server
{
// ...
};
}
In the *.cpp files (including main.cpp), make sure you have a corresponding using directive, such as:
using namespace MyNamespace;
Alternatively, you can fully-qualify the names when you use them, e.g. MyNamespace::Server instead of just Server.
As a side note, unnamed namespaces are actually valid and sometimes very useful. When the compiler sees a namespace without a name, it comes up with a unique internal name for it, and immediately follows it with a hidden using namespace ... directive. That's very useful for things which you want to define and use only within a single *.cpp file, because it can help avoid naming conflicts. (An older way to do something similar involved the static keyword).
As a rule though, don't use unnamed namespaces in a header file.

Struct defined in header and included in two source codes is only defined in one

I have a struct defined in a header file with three other files that #include that header file. One is another header(queue.h) file that defines a very basic hash table and the other two are source codes where one is defining the functions from the hash table header(queue.cpp) and the other contains main(p2.cpp).
The problem that I'm having is that the struct seems to work fine in p2.cpp but in queue.h the compiler is telling me that the struct is undefined.
Here is p2.h containing the struct definition.
#ifndef __P2_H__
#define __P2_H__
#define xCoor 0
#define yCoor 1
#include <iostream>
#include <string>
#include "queue.h"
#include "dlist.h" //linked list which I know works and is not the problem
using namespace std;
struct spot {
float key[2];
string name, category;
};
#endif /* __P2_H__ */
I have queue.h included in this header so that I only have to include p2.h in p2.cpp.
Here is p2.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "p2.h"
using namespace std;
int main () {
cout << fixed;
cout << setprecision (4);
Queue hashTable;
spot *spot1 = new spot;
spot1->key[xCoor] = 42.2893;
spot1->key[yCoor] = -83.7391;
spot1->name = "NorthsideGrill";
spot1->category = "restaurant";
hashTable.insert(spot1);
Dlist<spot> test = hashTable.find(42.2893, -83.7391);
while (!test.isEmpty()) {
spot *temp = test.removeFront();
cout << temp->key[xCoor] << " " << temp->key[yCoor] << " " << temp->name << " " << temp->category << endl;
delete temp;
}
return 0;
}
Places and item in the hash table and takes it back out.
Here is queue.h
#ifndef __QUEUE_H__
#define __QUEUE_H__
#include <iostream>
#include <string>
#include "dlist.h"
#include "p2.h"
using namespace std;
class Queue {
// OVERVIEW: contains a dynamic array of spaces.
public:
// Operational methods
bool isEmpty();
// EFFECTS: returns true if list is empy, false otherwise
void insert(spot *o);
// MODIFIES this
// EFFECTS inserts o into the array
Dlist<spot> find(float X, float Y);
// Maintenance methods
Queue(); // ctor
~Queue(); // dtor
private:
// A private type
int numInserted;
int maxElts;
Dlist <spot>** queue;
// Utility methods
//Increases the size of the queue.
void makeLarger();
int hashFunc(float X, float Y, int modNum);
};
#endif /* __QUEUE_H__ */
Here is queue.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "queue.h"
using namespace std;
bool Queue::isEmpty() {
return !numInserted;
}
void Queue::insert(spot *o) {
if (numInserted >= maxElts) {
makeLarger();
}
int index = hashFunc(o->key[xCoor], o->key[yCoor], maxElts);
queue[index] -> insertFront(o);
}
Queue::Queue() {
numInserted = 0;
maxElts = 1000;
queue = new Dlist<spot>*[maxElts];
for (int i = 0; i < maxElts; i++) {
queue[i] = new Dlist<spot>;
}
}
Queue::~Queue() {
for (int i = 0; i < maxElts; i++) {
delete queue[i];
}
delete[] queue;
}
void Queue::makeLarger() {
Dlist <spot>** temp = queue;
queue = new Dlist <spot>*[maxElts*2];
for (int i = 0; i < maxElts*2; i++) {
queue[i] = new Dlist<spot>;
}
for (int i = 0; i < maxElts; i++) {
while (!temp[i] -> isEmpty()) {
spot *spotTemp = temp[i] -> removeFront();
int index = hashFunc(spotTemp->key[xCoor], spotTemp->key[yCoor], maxElts*2);
queue[index] -> insertFront(spotTemp);
}
}
for (int i = 0; i < maxElts; i++) {
delete temp[i];
}
delete[] temp;
maxElts *= 2;
}
int Queue::hashFunc(float X, float Y, int modNum) {
return ((int)(10000*X) + (int)(10000*Y))%modNum;
}
Dlist<spot> Queue::find(float X, float Y) {
Dlist<spot> result;
Dlist<spot> *temp = new Dlist<spot>;
int index = hashFunc(X, Y, maxElts);
while (!queue[index] -> isEmpty()) {
spot *curSpot = queue[index] -> removeFront();
if ((curSpot->key[xCoor] == X) && (curSpot->key[yCoor] == Y)) {
result.insertFront(new spot(*curSpot));
}
temp -> insertFront(curSpot);
}
delete queue[index];
queue[index] = temp;
return result;
}
I believe that the problem is in my queue.h file because it's where I get all of the errors like "spot has not been declared". Every time spot appears in queue.h I have at least one error. I searched around for anything like this but all I could find was people trying to share one instance of a struct across multiple source files, or the obvious question of putting a struct in a header and including that header across multiple source files(which is what I'm doing but my problem seems to be a rather unique one).
You are including queue.h within the header that actually defines spot, so by the point the file is actually included spot has not been defined yet.
For your scope guards, note that identifiers starting with a double underscore are reserved by the implementation, don't use them.
And this is a poor choice even in plain C:
#define xCoor 0
#define yCoor 1
use this instead:
enum {
xCoor = 0
, yCoor = 1
};
Ok first never ever using "using" clauses in header files (it destroys the purposes of namespaces)
2nd provide a complete example that fails to compile
In addition to what others have said, you also have a circular reference error, which can also lead to similar undefined symbol errors. You have queue.h include p2.h, which includes queue.h.

Adding a string or char array to a byte vector

I'm currently working on a class to create and read out packets send through the network, so far I have it working with 16bit and 8bit integers (Well unsigned but still).
Now the problem is I've tried numerous ways of copying it over but somehow the _buffer got mangled, it segfaulted, or the result was wrong.
I'd appreciate if someone could show me a working example.
My current code can be seen below.
Thanks, Xeross
Main
#include <iostream>
#include <stdio.h>
#include "Packet.h"
using namespace std;
int main(int argc, char** argv)
{
cout << "#################################" << endl;
cout << "# Internal Use Only #" << endl;
cout << "# Codename PACKETSTORM #" << endl;
cout << "#################################" << endl;
cout << endl;
Packet packet = Packet();
packet.SetOpcode(0x1f4d);
cout << "Current opcode is: " << packet.GetOpcode() << endl << endl;
packet.add(uint8_t(5))
.add(uint16_t(4000))
.add(uint8_t(5));
for(uint8_t i=0; i<10;i++)
printf("Byte %u = %x\n", i, packet._buffer[i]);
printf("\nReading them out: \n1 = %u\n2 = %u\n3 = %u\n4 = %s",
packet.readUint8(),
packet.readUint16(),
packet.readUint8());
return 0;
}
Packet.h
#ifndef _PACKET_H_
#define _PACKET_H_
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
using namespace std;
class Packet
{
public:
Packet() : m_opcode(0), _buffer(0), _wpos(0), _rpos(0) {}
Packet(uint16_t opcode) : m_opcode(opcode), _buffer(0), _wpos(0), _rpos(0) {}
uint16_t GetOpcode() { return m_opcode; }
void SetOpcode(uint16_t opcode) { m_opcode = opcode; }
Packet& add(uint8_t value)
{
if(_buffer.size() < _wpos + 1)
_buffer.resize(_wpos + 1);
memcpy(&_buffer[_wpos], &value, 1);
_wpos += 1;
return *this;
}
Packet& add(uint16_t value)
{
if(_buffer.size() < _wpos + 2)
_buffer.resize(_wpos + 2);
memcpy(&_buffer[_wpos], &value, 2);
_wpos += 2;
return *this;
}
uint8_t readUint8()
{
uint8_t result = _buffer[_rpos];
_rpos += sizeof(uint8_t);
return result;
}
uint16_t readUint16()
{
uint16_t result;
memcpy(&result, &_buffer[_rpos], sizeof(uint16_t));
_rpos += sizeof(uint16_t);
return result;
}
uint16_t m_opcode;
std::vector<uint8_t> _buffer;
protected:
size_t _wpos; // Write position
size_t _rpos; // Read position
};
#endif // _PACKET_H_
Since you're using an std::vector for your buffer, you may as well let it keep track of the write position itself and avoid having to keep manually resizing it. You can also avoid writing multiple overloads of the add function by using a function template:
template <class T>
Packet& add(T value) {
std::copy((uint8_t*) &value, ((uint8_t*) &value) + sizeof(T), std::back_inserter(_buffer));
return *this;
}
Now you can write any POD type to your buffer.
implicitly:
int i = 5;
o.write(i);
or explictly:
o.write<int>(5);
To read from the buffer, you will need to keep track of a read position:
template <class T>
T read() {
T result;
uint8_t *p = &_buffer[_rpos];
std::copy(p, p + sizeof(T), (uint8_t*) &result);
_rpos += sizeof(T);
return result;
}
You will need to explicitly pass a type parameter to read. i.e.
int i = o.read<int>();
Caveat: I have used this pattern often, but since I am typing this off the top of my head, there may be a few errors in the code.
Edit: I just noticed that you want to be able to add strings or other non-POD types to your buffer. You can do that via template specialization:
template <>
Packet& add(std::string s) {
add(string.length());
for (size_t i = 0; i < string.length(); ++i)
add(string[i]);
return *this;
}
This tells the compiler: if add is called with a string type, use this function instead of the generic add() function.
and to read a string:
template <>
std::string read<>() {
size_t len = read<size_t>();
std::string s;
while (len--)
s += read<char>();
return s;
}
You could use std::string as internal buffer and use append() when adding new elements.
Thus adding strings or const char* would be trivial.
Adding/writing uint8 can be done with casting it to char, writing uint16 - to char* with length sizeof(uint16_t).
void write_uint16( uint16_t val )
{
m_strBuffer.append( (char*)(&var), sizeof(val) );
}
Reading uint16:
uint16_t read_int16()
{
return ( *(uint16_t*)(m_strBuffer.c_str() + m_nOffset) );
}
You appear to be attempting to print ten bytes out of the buffer when you've only added four, and thus you're running off the end of the vector. This could be causing your seg fault.
Also your printf is trying to print a character as an unsigned int with %x. You need to use static_cast<unsigned>(packet._buffer[i]) as the parameter.
Stylistically:
Packet packet = Packet(); could potentially result in two objects being constructed. Just use Packet packet;
Generally try to avoid protected attributes (protected methods are fine) as they reduce encapsulation of your class.