List stl C++ Struct - c++

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

Related

syntax error when using ifstream

It's been a long time since I've done any C++. What's wrong with this code?
#include <iostream>
#include <fstream>
using namespace std;
main()
{
ifstream& ifs("foo.txt");
}
Gives:
$ g++ foo.cc
foo.cc: In function ‘int main()’:
foo.cc:7:25: error: invalid initialization of non-const reference of type ‘std::ifstream& {aka std::basic_ifstream<char>&}’ from an rvalue of type ‘const char*’
ifstream& ifs("foo.txt");
You used a & when you should not have done.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs("foo.txt");
}
Passing values by reference isn't done in the variable declaration, but instead in the parameter list of the function using the ifstream object. For example, your function main might look like this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs("foo.txt");
myFunction(ifs);
}
and your called function should look like this:
void myFunction(std::ifstream& in_stream)
{
// ...
}
If you need the C++11 reference type (which I doubt, but maybe), try this:
ifstream ifs("foo.txt.");
std::ref<std::ifstream> ifs_ref(ifs);
That works in a lot of cases where doing a regular by-ref wouldn't.
semantically, a reference is a pointer. so your code doesn't compile for the same reason this code doesn't:
main()
{
ifstream* ifs("foo.txt");
}
as others have said, you want to create an object of type ifstream. not a reference (nor a pointer) to it.

error: ‘struct sigevent’ has no member named ‘sigev_notify_thread_id’

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.

BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error

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.

defining a hash set with a compare function

I am trying to do a very simple task of defining a new type of hash set which has a compare function as well.
using namespace std;
#include <iostream>
#include <ext/hash_set>
#include <hash_set>
#include <functional>
#include <hash_compare>
typedef __gnu_cxx::hash_set<int, hash_compare<int, less<int> > > hashcomp;
int main(int argc, char * const argv[]) {
}
Error: hash_compare is not defined (line 7)
Error: expected unqualified-id before ">" token (line 7)
Error: template argument 2 is invalid. (line 7)
Seeing the error, I guess you haven't included <functional> as you're using std::less<int> in your code. I'm assuming that less<int> in your code is actually std::less<int>.
EDIT:
The error message clearly says
Error: hash_compare is not defined.
What else do you want? Include the header file which defines hash_compare.
Your requirements are contradictory. A hash table has a random order, by definition.

error: expected `;' before '{' token - What is the cause?

Here is my implementation file:
using namespace std;
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
//----------------Constructor----------------//
Canvas::Canvas() //line 10
{
Title = "";
Nrow = 0;
Ncol = 0;
image[][100]; // line 15
position.r = 0;
position.c = 0;
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
The errors I'm getting are these:
proj05.canvas.cpp: In function 'std::istream& operator>>(std::istream&,
Canvas&)':
proj05.canvas.cpp:11: error: expected `;' before '{' token
proj05.canvas.cpp:22: error: a function-definition is not
allowed here before '{' token
proj05.canvas.cpp:24: error: expected `}' at end of input
proj05.canvas.cpp:24: error: expected `}' at end of input
These seem like simple syntax errors, but I am not sure what's wrong. Could someone decode these for me? I'd really appreciate it, thanks for your time!
EDIT
Here is the definition of Canvas in my .h file:
#ifndef CANVAS_H
#define CANVAS_H
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
class Canvas
{
public:
Canvas(); void Paint(int R, int C, char Color);
const int Nrow;
const int Ncol;
string Title;
int image[][100];
stack<int> path;
struct PixelCoordinates
{
unsigned int r;
unsigned int c;
} position;
};
#endif
"proj05.canvas.h" i bet the problem is there. may be no ; after class def
You must use initializer list to initialize const-members
Try this:
#include <iostream>
#include <iomanip>
#include <string>
#include <stack> //line 5
#include "proj05.canvas.h"
using namespace std;
//----------------Constructor----------------//
Canvas::Canvas():Nrow(),Ncol() // Initializer list
{
Title = "";
//initialize image[][] correctly, your way is syntactically incorrect
position.r = 0; //correction here
position.c = 0; // and here
}
//-------------------Paint------------------// line 20
void Canvas::Paint(int R, int C, char Color)
{
cout << "Paint to be implemented" << endl;
}
Few things:
1
PixelCoordinates.r = 0;
PixelCoordinates.c = 0;
should be:
position.r = 0;
position.c = 0;
2
image has already been declared. What is this:
image[][];
It sounds like you forgot to put a semicolon after your class definition. Look in "proj05.canvas.h". You should see something like:
class Canvas{
...
};
One thing that catches my eye as wrong/weird is image[][]. That does not really do anything. Also, I do not believe you can assign to constant member outside of a ctor list.
Finally, your assignment to PixelCoordinates is completely in error. You've created a local struct definition, but have not made a member that uses it, therefore you cannot assign anything at all to it - especially the struct's title. That would really confuse a compiler.
Yikes.
(Not an answer to your specific problem, but...)
You should also remove the
using std;
That has no business in a .h file.
I am going to guess the oddly formatted .h file may be a problem. It is legal for a filesystem of course, but it could be that. Also ensure you have the ending semicolon on the class.
You need to have both dimensions filled in for the array you have (probably a horrible design to use that int he class anyway...)
Whatever the reason for other errors is, the memeber definition int image[][100] is illegal. Non-static data members of the class cannot be declared with incomplete types. All dimensions of an array must be specified explicitly.