Undefined reference errors c++ - 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.

Related

Implementing SystemC TLM Testbench Build Fail

I am trying to implement a SystemC basic TLM test bench for an adder module I created using basic simple_initiator_socket and simple_target_socket.
Currently the build is failing and I am having trouble diagnosing why.
Here are the implementations for the three main modules, the adder, the test bench, and the main module that instantiates both and initiates dataflow.
main.cc
#include "systemc.h"
#include "tlm_utils/simple_initiator_socket.h"
#include "tlm_utils/simple_target_socket.h"
#include "tlm_utils/tlm_quantumkeeper.h"
using namespace sc_core;
using namespace sc_dt;
using namespace std;
#include "test_bench.h"
#include "adder.h"
SC_MODULE(Top)
{
test_bench *tb;
adder *ad;
sc_signal<bool> rst;
Top(sc_module_name name) :
rst("rst")
{
tb = new test_bench("test_bench");
ad = new adder("adder");
tb->socket.bind(ad->socket);
}
};
int sc_main(int argc, char *argv[])
{
Top *top = new Top("Top");
}
test_bench.cc
#define SC_INCLUDE_DYNAMIC_PROCESS
#include "tlm_utils/simple_initiator_socket.h"
#include "tlm_utils/simple_target_socket.h"
using namespace sc_core;
using namespace std;
using namespace sc_dt;
#include "test_bench.h"
#include <fstream>
#include <iostream>
test_bench::test_bench(sc_module_name name):
sc_module(name), socket("socket")
{
SC_THREAD(run_tests);
}
void test_bench::run_tests()
{
ifstream infile("../adder.golden.dat");
ofstream ofs;
ofs.open("../adder.dat", ofstream::out | ofstream::app);
while(infile >> data[0] >> data[1])
{
tlm::tlm_generic_payload *trans = new tlm::tlm_generic_payload;
sc_time delay = sc_time(10, SC_NS);
trans->set_data_ptr((unsigned char*)data);
socket->b_transport(*trans, delay);
ofs << data[0] << data[1] << data[2];
delete trans;
}
infile.close();
ofs.close();
printf ("Comparing against output data \n");
if (system("diff -w sha1.dat sha1.golden.dat"))
{
cout << "*******************************************" << endl;
cout << "FAIL: Output DOES NOT match the golden output" << endl;
cout << "*******************************************" << endl;
}
else
{
cout << "*******************************************" << endl;
cout << "PASS: The output matches the golden output!" << endl;
cout << "*******************************************" << endl;
}
}
adder.cc
#define SC_INCLUDE_DYNAMIC_PROCESS
#include "tlm_utils/simple_initiator_socket.h"
#include "tlm_utils/simple_target_socket.h"
using namespace sc_core;
using namespace std;
#include "adder.h"
adder::adder(sc_module_name name)
: sc_module(name), socket("socket")
{
socket.register_b_transport(this, &adder::b_transport);
socket.register_transport_dbg(this, &adder::transport_dbg);
}
void adder::b_transport(tlm::tlm_generic_payload& trans, sc_time& delay)
{
tlm::tlm_command cmd = trans.get_command();
sc_dt::uint64 addr = trans.get_address();
uint32_t *ptr = (uint32_t*)trans.get_data_ptr();
unsigned int len = trans.get_data_length();
unsigned char *byt = trans.get_byte_enable_ptr();
unsigned int wid = trans.get_streaming_width();
addend1 = *ptr;
addend2 = *(ptr++);
add();
memcpy(ptr + sizeof(uint32_t) * 2, (char*) &sum, sizeof(uint32_t));
}
unsigned int adder::transport_dbg(tlm::tlm_generic_payload& trans)
{
return 0;
}
void adder::add()
{
sum = addend1 + addend2;
}
Here is the error I am seeing upon compilation.
In file included from
/home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/sysc/kernel/sc_module.h:35:0,
from /home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/systemc:74,
from /home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/tlm:23,
from /home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/tlm_utils/simple_initiator_socket.h:23,
from /home/test_benches/adder/test_bench.cc:3:
/home/test_benches/adder/test_bench.cc:
In constructor ‘test_bench::test_bench(sc_core::sc_module_name)’:
/home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/sysc/kernel/sc_module.h:463:29:
error: ‘SC_CURRENT_USER_MODULE’ has not been declared
SC_CURRENT_USER_MODULE, \
/home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/sysc/kernel/sc_process.h:151:46: note: in definition of macro ‘SC_MAKE_FUNC_PTR’
static_cast(&callback_tag::func)
/home/epi/jfrye_xilinx/SystemC/systemc-2.3.2/include/sysc/kernel/sc_module.h:461:5:
note: in expansion of macro ‘declare_thread_process’
declare_thread_process( func ## _handle, \
/home/test_benches/adder/test_bench.cc:17:2: note: in expansion of
macro ‘SC_THREAD’ SC_THREAD(run_tests);
make: ***
[/home//test_benches/adder/obj/test_bench.o]
Error 1
My best guess is that I did not set up the sockets correctly. The test bench has a simple_initiator_socket and the adder has a simple_target_socket. Do I need to register the simple_target_socket with a b_transport method for the module? I did so in the initiator but in the tutorial below I did not see a requirement to do so for the target. My guess was the dataflow was like this:
simple_initiator_socket (member of test_bench) registered to b_transport method of module and simple_target_socket of another module (in top module)
Initiator module (test_bench) sets up tlm_generic_payload with data it needs to send to target (adder)
b_transport method of simple_initiator_socket (member of test_bench) called with tlm_generic_payload being passed (with addends for adder)
Target socket (target) receives and decodes tlm_generic_payload (addend values) that was passed.
Target socket (adder) performs operations (adds decoded addends) and modifies the tlm_generic_payload (passed by value) (by writing the computed sum back to the payload memory)
Initiator (test_bench) looks at modified tlm_generic_payload (now contains sum) and does some process (checks against theoretical sum)
I was trying to follow this example.
https://www.doulos.com/knowhow/systemc/tlm2/tutorial__1/
UPDATE
test_bench.h
class test_bench:
public sc_core::sc_module
{
public:
tlm_utils::simple_initiator_socket<test_bench> socket;
sc_out<bool> irq;
test_bench(sc_core::sc_module_name name);
void run_tests();
private:
uint32_t data[3];
};
There are two ways to declare modules in SystemC.
The first one is through using SC_MODULE macro:
SC_MODULE(mymodule) {
SC_CTOR(mymodule)
{
}
};
And the second one without it:
class mymodule : public sc_core::sc_module {
SC_HAS_PROCESS(mymodule);
public:
mymodule(sc_core::sc_module_name)
{ }
};
I would prefer the second one because:
It avoids those nasty macros as much as possible.
It allows you to inherit from another module.
Now why you need SC_MODULE or SC_HAS_PROCESS macros. The reason is that macros SC_METHOD and SC_THREAD need to know type of module they are being used from to do their job. Since SystemC is based on old revision of C++ language released in 1998, there was no way to do this automatically. So helper macro SC_HAS_PROCESS was defined as:
#define SC_HAS_PROCESS(user_module_name) typedef user_module_name SC_CURRENT_USER_MODULE
This allows SC_METHOD and SC_THREAD to use SC_CURRENT_USER_MODULE as synonym for module they are being used in. Macro SC_MODULE already uses SC_HAS_PROCESS behind the curtain.
Another advise - if you are using C++11 compatible compiler you can declare a helper macro for yourself:
#define DECL(name, ...) name{#name, __VA_ARGS__}
This can help you declare named objects without typing their name twice:
sc_in<bool> DECL(clk);
Any error message mentioning this port with contain proper name for it.
Can also be used in constructor to initialize member field:
mymodule(sc_core::sc_module_name)
: DECL(clk)
{
}

' was not declared in this scope' 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.

using enum for an array index in c++

#include <stdlib.h>
#include <stdio.h>
using namespace std;
void main(){
char *resolutions[] = { "720x480", "1024x600", "1280x720", "1920x1080" };
int x = 0;
enum ResMode
{
p480,
p600,
p720,
p1080
};
ResMode res = p480;
printf("\nPlease enter the resolution you wish to use now by entering a number");
printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");
gets(res);
printf("\nThe resolution you have selected is %s", resolutions[res]);
}
so basically i want to be able to press 1 and have it select p600 from enum and out put it as 1024x600 in the next line. I am getting a type conversion error.
How can i fix this?
Looks like you want to associate some items with other items. Usually associations are described in lookup tables or maps.
std::map<ResMode, std::string> map_table =
{
{p480, string("720x480")},
{p600, string("1024x600")},
{p720, string("1280x720")},
{p1080, string("1920x1080")},
};
int main(void)
{
cout << map_table[p480] << "\n";
return EXIT_SUCCESS;
}
Likewise, you can map menu selections to enums.
Edit 1
std::map<unsigned int, ResMode> selection_map =
{
{0, p480}, {1, p600}, {2, p720}, {3, p1080},
};
int main(void)
{
cout << "\n"
<< "Please enter the resolution you wish to use now by entering a number\n"
<<"480p[0], 600p[1], 720p[2], 1080p[3]";
unsigned int selection = 0;
cin >> selection;
if (selection < 4)
{
Resmode resolution_index = selection_map[selection];
cout << "You chose: "
<< map_table[resolution_index]
<< "\n";
}
return EXIT_SUCCESS;
}
int's are not implicitly convertible to an enum. You will have to read in an int and then cast it yourself. Example,
int resInt;
scanf("%d", &resInt);
res = static_cast<ResMode>(resInt);//Note that this does not do bound checking.
You can use "scanf" instead of "gets", something like this:
scanf("%d",&res); // I recommend use scanf_s
Or the iostream library with std::cin. But after taking the input, always, check if the input is the correct one.
As otehrs pointed out, there is no direct way of doing this. However, there are some recipes/tricks that you can use. I modified your code as follows:
#include <stdlib.h>
#include <stdio.h>
#define SOME_ENUM(DO) \
DO(_720x480) \
DO(_1024x600) \
DO(_1280x720) \
DO(_1920x1080)
#define MAKE_ENUM(VAR) VAR,
enum class RESOLUTIONS
{
SOME_ENUM(MAKE_ENUM)
};
#define MAKE_STRINGS(VAR) #VAR,
const char* const
RESOLUTION_NAMES[] =
{
SOME_ENUM(MAKE_STRINGS)
};
const char *
GET_RESOLUTION_NAME(RESOLUTIONS type)
{
return RESOLUTION_NAMES[static_cast<int>(type)];
}
int
GET_RESOLUTION_VALUE(RESOLUTIONS type)
{
return static_cast<int>(type);
}
RESOLUTIONS
GET_RESOLUTION(int i)
{
return static_cast<RESOLUTIONS>(i);
}
using namespace std;
int main(){
printf("\nPlease enter the resolution you wish to use now by entering a number");
printf("\n480p[0], 600p[1], 720p[2], 1080p[3]");
int res_type;
cin >> res_type;
RESOLUTIONS selected_res = GET_RESOLUTION(res_type);
printf("\nThe resolution you have selected is %s\n\n", GET_RESOLUTION_NAME(selected_res));
return 0;
}
Sorry for not providing an explanation, as I have to go now. This recipe can be found here. The code works and compiles for c++11.

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.

Problems returning vector stack reference

I am working on an application that builds a vector of structs for items in a given directory and returns a reference of the vector for it to be read, I receive the following errors when attempting to compile the example code below:
1. 'class std::vector<indexStruct, std::allocator<indexStruct> >' has no member named 'name'
2. no matching function for call to `std::vector<indexStruct, std::allocator<indexStruct> >::push_back(std::vector<indexStruct, std::allocator<indexStruct> >&)'
exampleApp.cpp
#include "exampleApp.h"
exampleApp::exampleApp()
{
this->makeCatalog();
}
char* findCWD()
{
char* buffer = new char[_MAX_PATH];
return getcwd(buffer, _MAX_PATH);
}
void exampleApp::makeCatalog()
{
char* cwd = this->findCWD();
vector<indexStruct> indexItems;
this->indexDir(cwd, indexItems);
}
void exampleApp:indexDir(char* dirPath, vector<indexStruct>& indexRef)
{
DIR *dirPointer = NULL;
struct dirent *dirItem = NULL;
vector<indexStruct> indexItems;
vector<indexStruct> indexItem;
try
{
if ((dirPointer = opendir(dirPath)) == NULL) throw 1;
while (dirItem = readdir(dirPointer))
{
if (dirItem == NULL) throw 2;
if (dirItem->d_name[0] != '.')
{
indexItem.name = dirItem->d_name;
indexItem.path = dirPath;
indexItems.push_back(indexItem);
indexItem.clear();
}
}
indexRef.swap(indexItems);
closedir(dirPointer);
}
catch(int errorNo)
{
//cout << "Caught Error #" << errorNo;
}
}
exampleApp.h
#ifndef EXAMPLEAPP_H
#define EXAMPLEAPP_H
#include <iostream.h>
#include <dirent.h>
#include <stdlib.h>
#include <vector.h>
using namespace std;
struct indexStruct
{
char* name;
char* path;
};
class exampleApp
{
public:
exampleApp();
private:
char* findCWD();
void makeCatalog();
void indexDir(char* dirPath, vector<indexStruct>& indexRef);
};
#endif
What am I doing wrong here, and is there a better way going about this?
You've made 'indexItem' a vector, you probably just want it to be the type you want to put in 'indexItems'. Also, I'd create the new struct in your loop:
while (dirItem = readdir(dirPointer))
{
if (dirItem == NULL) throw 2;
if (dirItem->d_name[0] != '.')
{
indexStruct indexItem;
indexItem.name = dirItem->d_name;
indexItem.path = dirPath;
indexItems.push_back(indexItem);
}
}
You are defining a vector called indexItem:
vector<indexStruct> indexItem;
This is just an array. So the following lines must be changed to reference a specific element of the vector:
indexItem.name = dirItem->d_name;// should be indexItem[..].name or indexItem.at(..).name
indexItem.path = dirPath; // same as above!