The following "toy" code represents a problem I am having in a larger code base using POSIX timers.
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <iostream>
using namespace std;
int main()
{
struct sigevent sevp;
long threadId = 5;
sevp.sigev_notify = SIGEV_THREAD_ID;
sevp.sigev_notify_thread_id = threadId;
return 0;
}
When I try to compile it using g++ on a Linux machine I get the error:
error: ‘struct sigevent’ has no member named ‘sigev_notify_thread_id’
Is there a reason why? This leads me to believe that the sigevent struct has a member called sigev_notify_thread_id.
Changing sevp.sigev_notify_thread_id to sevp._sigev_un._tid fixed my problem. You can see the definition on line 295 here.
Thanks to #Duck for the helpful comment.
Related
I am writing a C++ program on the Raspberry Pi. I am using the ctime library to get the current time and date to make it the title of a text file. For example, where I am the current date and time is 14:51 on the 23rd October 2015. So the name of the text file will be 20151023_14_51.txt. Here is the code:
FILE *f;
main(int argc, char *argv[]){
char dateiname[256]="";
time_t t = time(0);
struct tm * now = localtime(&t);
//Create and open file
sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt",
now->tm_year+1900,
now->tm_mon+1,
now->tm_mday,
now->tm_hour,
now->tm_min;
f = fopen(dateiname, "w");
My problem is that when I try to compile the program with gcc I am getting errors of the following nature:
error: invalid use of incomplete type 'struct main(int, char**)::tm'
error: forward decleration of 'struct main(int, char**)::tm'
I also get this error at the beginning:
error: 'localtime' was not declared in this scope
I did some research and found that people with similar problems weren't including sys/time.h but I have that included. Here is what I include:
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
Does anyone have any idea of what could be causing these errors or if I am missing anything? Thanks.
The struct tm is defined by either #include <time.h>, or by #include <ctime> and using std::tm for the name.
I am trying to create a lazy function from a template function following the Boost::phoenix documentation. The code looks like this
#include <iostream>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>
using namespace boost;
using namespace boost::phoenix;
namespace demo
{
bool func(double a,double b)
{
return bool(a > b);
}
}
BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)
int main(int argc,char **argv)
{
namespace pl = boost::phoenix::placeholders;
auto comperator = func(pl::arg1,pl::arg2);
std::cout<<comperator(1.2,12.4)<<std::endl;
std::cout<<comperator(0.5,0.1)<<std::endl;
}
This is virtually one of the examples from the BOOST documentation. Storing this file as mk_lazy1.cpp and try to compile gives
$ g++ -omk_lazy1 mk_lazy1.cpp
mk_lazy1.cpp:26:1: error: template argument 1 is invalid
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope
I use gcc-4.7 on a Debian testing system. An honestly I am a bit lost as I have absolutely no idea what is wrong here (as I said, this is virtually a word by word copy of one of the examples provided by the Boost documentation).
Does anyone have a good idea?
Remove using namespaces and all will work fine.
Or write using namespaces AFTER adapt macro and all will work fine too.
Or put macro into unnamed namespace.
I am having a problem with the while loop inserting data into a stl list from text file. Could you please help me understand my errors? Thanks a lot.
Errors are
server3.cpp: In function ‘int main(int, char**)’:
server3.cpp:43:11: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
server3.cpp:74:15: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:74:25: error: ‘class std::list<Record>’ has no member named ‘firstName’
server3.cpp:74:42: error: ‘class std::list<Record>’ has no member named ‘lastName’
server3.cpp:75:12: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:76:17: error: no match for ‘operator[]’ in ‘hashtable[hash]’
server3.cpp:76:50: error: expected primary-expression before ‘)’ token
Code follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <vector>
#include <list>
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE =100;/*size of hashTable*/
/*Struct representing the record*/
struct Record
{
int id;
char firstName[100];
char lastName[100];
} rec;
/*Structure representing a single cell*/
class Cell
{
std:: list<Record> recs;
pthread_mutex_t lock;
};
/* The actual hash table */
std::list<Cell> hashtable;
int main (int argc, char * argv[])
{
ifstream indata; /* indata is like a cin*/
indata.open("fileName"); /* opens the file*/
list <Record> rec;/*create an object*/
int hash;
while ( !indata.eof() ) /* keep reading until end-of-file*/
{
indata>> rec.id >> rec.firstName >> rec.lastName;
hash =rec.id % sizeof(hashtable);
hashtable [hash].listofrecords.push_back (Record);
}
indata.close();
return 0;
}
Most of them are telling you that list doesn't have no members, because you try to do
indata>> rec.id >> rec.firstName >> rec.lastName;
but rec is a list, not a Record.
hashtable[hash]
is also illegal (see the interface for std::list, and Record is a type, and you can't insert a type in a container, you can only insert objects:
...push_back (Record);
is illegal.
The code doesn't have just the occasional error we all make from time to time, but is fundamentally flawed. I suggest you start learning C++ (if that's what you're doing) from a good book.
Please note that you are creating a collection of records, but this means that you need to access an element of the collection before accessing the fields of the records contained there.
Here's a reference on using the list type:
http://www.cplusplus.com/reference/list/list/
There are at least three major problems with this line alone.
hashtable [hash].listofrecords.push_back (Record);
std::list doesn't have an operator[] so you can't use the [hash] subscripting.
No place in your program have you defined what listofrecords means.
You are trying to push_back a type named Record where an object is required.
Please find a good C++ book to get started with: The Definitive C++ Book Guide and List
I am having a very strange issue with stat.h
At the top of my code, I have declarations:
#include <sys\types.h>
#include <sys\stat.h>
And function prototype:
int FileSize(string szFileName);
Finally, the function itself is defined as follows:
int FileSize(string szFileName)
{
struct stat fileStat;
int err = stat( szFileName.c_str(), &fileStat );
if (0 != err) return 0;
return fileStat.st_size;
}
When I attempt to compile this code, I get the error:
divide.cpp: In function 'int FileSize(std::string)':
divide.cpp:216: error: aggregate 'stat fileStat' has incomplete type and cannot be defined
divide.cpp:217: error: invalid use of incomplete type 'struct stat'
divide.cpp:216: error: forward declaration of 'struct stat'
From this thread: How can I get a file's size in C?
I think this code should work and I cannot figure out why it does not compile. Can anybody spot what I am doing wrong?
Are your \'s supposed to be /'s or am I just confused about your environment?
UNIX MAN page:
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *restrict path, struct stat *restrict buf);
If you're on Windows (which I'm guessing you might be because of the \'s), then I can't help because I didn't even know that had stat.
I am having trouble deciphering these error messages from g++
../upenn-cis553/ls-routing-protocol/ls-routing-protocol.cc:533:29: error: variable ‘ns3::Ipv4RoutingTableEntry route’ has initializer but incomplete type
../upenn-cis553/ls-routing-protocol/ls-routing-protocol.cc:533:64: error: invalid use of incomplete type ‘struct ns3::Ipv4RoutingTableEntry’
Here is my ls-routing-protocol.h file:
#include "ns3/ipv4.h"
#include "ns3/ipv4-routing-protocol.h"
#include "ns3/ipv4-static-routing.h"
#include "ns3/object.h"
#include "ns3/packet.h"
#include "ns3/node.h"
#include "ns3/socket.h"
#include "ns3/timer.h"
#include "ns3/ping-request.h"
#include "ns3/penn-routing-protocol.h"
#include "ns3/ls-message.h"
#include <vector>
#include <map>
...
private:
...
Ptr<Ipv4StaticRouting> m_staticRouting;
...
And here the relevant snippet from the ls-routing-protocol.cc file:
#include "ns3/ls-routing-protocol.h"
#include "ns3/socket-factory.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/simulator.h"
#include "ns3/log.h"
#include "ns3/random-variable.h"
#include "ns3/inet-socket-address.h"
#include "ns3/ipv4-header.h"
#include "ns3/ipv4-route.h"
#include "ns3/uinteger.h"
#include "ns3/test-result.h"
#include <sys/time.h>
using namespace ns3;
void
LSRoutingProtocol::AuditRoutes ()
{
int i;
int n = m_staticRouting->GetNRoutes();
for (i=0; i < n; i++)
{
Ipv4RoutingTableEntry route = m_staticRouting->GetRoute(i); // ERROR
...
}
...
}
As some of you may tell, I am working with ns-3. I have looked up my error in many places, and most of the advice has been to properly declare a few structs. However, we are not directly using structs in this code (or at least not that I know of). I am starting to think that it's an issue with our use of smart pointers, but I'm not really sure.
Also, in case it is of any help: documentation for ipv4_static_routing.h
You need to #include <ipv4-routing-table-entry.h>. This is the first thing that you see when you look at the documentation of ns3::Ipv4RoutingTableEntry class.