c++ error: expected primary-expression before '&' token - c++

I'm learning c++ and I have the following code which gives an error in line 39 (fill_file() call). I've searched on the web for a solution but can't find why I get this error (expected primary-expression before '&' token).
#include <iostream>
#include <string>
#include <vector>
#include "../std_lib_facilities.h"
using namespace std;
struct Point {
double x;
double y;
};
void fill_file(vector<Point>& original_points) {
string outputfile="mydata.txt";
ofstream ost{outputfile};
if(!ost) error("Can't open outputfile ", outputfile);
for(int i=0;i<original_points.size();i++) {
ost << original_points[i].x << " " << original_points[i].y << endl;
}
}
int main() {
cout << "Please enter 3 points with a value: " << endl;
vector<Point> original_points;
Point p;
double x;
double y;
for(int i=0;i<3;i++) {
cin>>p.x;
cin>>p.y;
original_points.push_back(p);
}
cout << endl;
cout << endl << "Points: " << endl;
for(int i=0;i<original_points.size();i++) {
cout << original_points[i].x << " " << original_points[i].y << endl;
/* ost << original_points[i].x << " " << original_points[i].y << endl; */
}
cout << endl << endl;
fill_file(vector<Point>& original_points);
return 0;
}
What am I doing wrong? Thx for the help!!

You made a mistake when you called your fill_file function:
fill_file(vector<Point>& original_points);
must be called like this:
fill_file(original_points);

You made an error calling the function fill_file(). Currently you call it like this:
fill_file(vector<Point>& original_points);
This above, I presume is a copy paste error. What I thing you want to do is:
fill_file(original_points);
because original_points is the actual variable, not vector<Point>& original_points. As your error states:
expected primary-expression before '&' token
As seen above, you are putting a random l-value in the function call, and this is not allowed.

Related

reading pairs of data from a file

I am trying to read pairs of data from a file using the following code.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
//**** Opening data file ****
ifstream infile;
infile.open("reg_data_log.inp");
if (infile.is_open())
{
cout << "File successfully open" << endl;
}
else
{
cout << "Error opening file";
}
//**** Reading data ***
vector<pair<double, double> > proteins;
pair<double, double> input;
while (infile >> input.first >> input.second)
{
proteins.push_back(input);
}
//**** Show data in screen ****
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
}
when compiling I have the following message
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
"65:13: error: request for member ‘first’ in ‘i’, which is of non-class type ‘unsigned int’"
I cannot get my head around the problem. Can anybody help?
Look further at your loop
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
You are trying to access the property first of the integer value i. integers do not have that property it is a property of a pair object.
I think you are getting confused between iterating using iterator and indices. The simple fix is to use a range based for loop, as already suggested in comments.
for(auto d: proteins)
{
cout << d.first << ", " << d.second << endl;
}
Now what you have as is the element from the vector and not an integer. You can now access first and second.
If you cannot use range based for loop and auto, then you can use the old iterator for loop way.
for(vector<pair<double, double> >::iterator it = proteins.begin();
it != proteins.end();
++it)
{
cout << it->first << ", " << it->second << endl;
}
and someone else has already posted the how it can be done using indices, so I won't repeat that here.
As John Mopp mentions in his comment, indeed you arev referencing an int and not a pair-type. The following loop will most likely fix your issues:
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << proteins[i].first << ", " << proteins[i].second << endl;
}
I did not test this yet, but I am pretty convinced this solves your problem.

Statement cannot resolve address of overloaded function C++ indicators

Statement cannot resolve address of overloaded function.
I try to learn indicators in C++, how to make it work?
#include <iostream>
using namespace std;
int XD = 330;
int *iks;
int main()
{
iks = &XD;
cout << "Hello" << endl;
cout << iks; << endl;
cout << *iks; << endl;
return 0;
}
You have unnecessary semicolon in lines 13 & 14, should be:
cout << iks << endl;
cout << *iks << endl;
And, by the way, the thing you call indicators are really called pointers in English.

Floating Point Exception while reading from file

the program should read from 2 files (author.dat and citation.dat) and save them into a map and set;
first it reads the citationlist without problem, then it seems to properly read the authors and after it went through the whole list (author.dat) a floating point exception arises .. can't quite figure out why
seems to happen in author.cpp inside the constructor for authorlist
author.cpp:
#include <fstream>
#include <iostream>
#include "authors.h"
using namespace std;
AuthorList::AuthorList(char *fileName) {
ifstream s (fileName);
int idTemp;
int nrTemp;
string nameTemp;
try {
while (true){
s >> idTemp >> nrTemp >> nameTemp;
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << "WHILE-LOOP_END" << endl;
}
} catch (EOFException){}
}
author.h:
#ifndef CPP_AUTHORS_H
#define CPP_AUTHORS_H
#include <iostream>
#include <map>
#include <string>
#include "citations.h"
class Author {
public:
Author (int id, int nr, std::string name) :
articleID(id),
authorNR(nr),
authorName(name){}
int getArticleID() const {
return articleID;
}
std::string getAuthorName() const {
return authorName;
}
private:
int articleID;
int authorNR;
std::string authorName;
};
class AuthorList {
public:
AuthorList(char *fileName);
std::pair<std::multimap<int,Author>::const_iterator, std::multimap<int,Author>::const_iterator> findAuthors(int articleID) {
return authors.equal_range(articleID);
}
private:
std::multimap<int,Author> authors;
};
#endif //CPP_AUTHORS_H
programm.cpp:
#include <iostream>
#include <cstdlib>
#include "citations.h"
#include "authors.h"
#include "authorCitation.h"
using namespace std;
int main(int argc, char *argv[]){
CitationList *cl;
AuthorList *al;
//check if argv array has its supposed length
if (argc != 4){
cerr << "usage: programm article.dat citation.dat author.dat";
return EXIT_FAILURE;
}
//inserting citation.dat and author.dat in corresponding lists (article.dat not used)
cl = new CitationList(argv[2]);
al = new AuthorList(argv[3]);
try {
AuthorCitationList *acl;
acl->createAuthorCitationList(al,cl);
acl->printAuthorCitationList2File("authorcitation.dat");
} catch (EOFException){
cerr << "something went wrong while writing to file";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
All files:
https://drive.google.com/file/d/0B734gx5Q_mVAV0xWRG1KX0JuYW8/view?usp=sharing
I am willing to bet that the problem is caused by the following lines of code:
AuthorCitationList *acl;
acl->createAuthorCitationList(al,cl);
You are calling a member function using an uninitialized pointer. I suggest changing the first line to:
AuthorCitationList *acl = new AuthorCitationList;
Add any necessary arguments to the constructor.
While you are at it, change the loop for reading the data also. You have:
while (true){
s >> idTemp >> nrTemp >> nameTemp;
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << "WHILE-LOOP_END" << endl;
}
When you do that, you end up adding data once after the end of line has been reached. Also, you seem to have the last line in the wrong place. It seems to me that it should be outside the while loop.
You can use:
while (true){
s >> idTemp >> nrTemp >> nameTemp;
// Break out of the loop when reading the
// data is not successful.
if (!s){
cout << "IF-CLAUSE";
throw EOFException();
}
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;
You can simplify it further by using:
while (s >> idTemp >> nrTemp >> nameTemp){
cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;

Visual Studio 2013 programming "<<" is not matching operands?

I am getting an error saying the operand "<<" (right before times3(x) in the main function ) does not match the operand types being outputted in that line. What am I doing wrong? I searched for errors similar to it and found that its an inclusion error but i thought having would fix it. Also, countdown(seconds) in the main function is not being recognized and giving me an error. Why is that? The problems keep occurring when working with void.
'
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
using namespace std;
bool die(const string & msg);
double triple(double x);
double times9(double x);
void triple(double & result, double x);
void times3(double & x);
void countdown(unsigned seconds);
bool restore();
int main(){
double x;
cout << "x: " << endl;
cin >> x;
cout << "The triple of " << x << " is " << triple(x) << endl;
cout << "9 times of " << x << " is " << times9(x) << endl;
cout << "3 times of " << x << " is " << times3(x) << endl;
unsigned seconds;
cout << "seconds: " << endl;
cin >> seconds;
cout << countdown(seconds) << endl;
}
bool die(const string & msg){
cout << "Fatal error: " << msg << endl;
exit(EXIT_FAILURE);
}
double triple(double x){
return 3 * x;
}
double times9(double x){
return 3 * triple(x);
}
void triple(double & result, double x){
x = 3 * x;
}
void times3(double & x){
x = triple(x);
}
void countdown(unsigned & seconds){
unsigned count = seconds;
cin >> seconds || die("input failure");
for (unsigned i = seconds; i <= size; i--){
cout << i << endl;
}
cout << "Blast off! " << endl;
}
bool resotre(){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return cin.good();
}'
As mentioned in earlier answer, you need to change the return type of your function from void to the data type of variable your trying to print.
Another issue in your code is with function void countdown(unsigned & seconds)
Declaration and definition of the functions are different.
You have declared it as void countdown(unsigned seconds); but at the time of defining it you are using void countdown(unsigned & seconds). In declaration you are declaring it to take arguments by value but in definition you are making it to take arguments by reference.
Also in the for loop of the function countdown you have written
for (unsigned i = seconds; i <= 0; i--), this won't print any output, since your condition is i<=0, i think you tried to type i >= 0. :)
times3 returns void. Try:
times3(x);
cout << "3 times of " << x << " is " << x << endl;
Or have times3() return double instead of passing by reference.
double times3(double x);

error C2228: left of '.printStats' must have class/struct/union

So I recently decided to pick up programming again and went with C++. Tried to make an adventurer class, but I seem to be running into some trouble. Here are my files:
Adventurer.h:
#ifndef __Adventurer_H_INCLUDED__ //if Adventurer.h hasn't been included yet...
#define __Adventurer_H_INCLUDED__ //#define this so the compiler knows it has been included
class Adventurer
{
private:
int hp, mp, str, agi, magic, armour;
public:
Adventurer(){}
void printStats();
}
#endif
Adventurer.cpp:
#include <iostream>
#include "Adventurer.h"
Adventurer::Adventurer()
{
hp = 50;
mp = 25;
str = 5;
agi = 5;
magic = 5;
armour = 5;
}
void Adventurer::printStats()
{
cout << "HP = " << hp << "\n\n";
cout << "MP = " << mp << "\n\n";
cout << "str = " << str << "\n\n";
cout << "agi = " << agi << "\n\n";
cout << "magic = " << magic << "\n\n";
cout << "armour = " << armour << "\n\n";
}
RPG_Game.cpp:
// my first program in C++
#include <iostream>
#include <string>
#include "Adventurer.h"
;using namespace std;
int main()
{
cout << "Hello Adventurer! What is your name? \n";
string advName;
cin >> advName;
cout << "\nYour name is " << advName << "!";
Adventurer *adv = new Adventurer();
cout << adv.printStats();
delete adv;
system(pause);
}
Let's look at the errors in your code
First, in your Adventurer.h, put a semicolon (;) after the class.
Next, in that same class, you have
Adventurer(){}
change this to
Adventurer();
Then, in your RPG_Game.cpp , change
cout << adv.printStats();
to
adv->printStats() ;
When using pointers, you need to use -> and not .
And lastly,
system(pause);
should be
system( "pause" );
Now, try running your code.
Also, you might find this helpful.