error: expected unqualified-id before ‘.’ token - std :: vector - c++

Code:
#include <vector>
#include <iostream>
typedef struct
{
std :: string latitude;
std :: string longitude;
} coordinate;
std :: vector <coordinate> previousPoints;
int main ()
{
coordinate.latitude = latitude;
coordinate.longitude = longitude;
previousPoints.push_back (coordinate);
return 0;
}
Output:
anisha#linux-y3pi:~> g++ -Wall demo.cpp
demo.cpp: In function ‘int main()’:
demo.cpp:14:12: error: expected unqualified-id before ‘.’ token
demo.cpp:15:12: error: expected unqualified-id before ‘.’ token
demo.cpp:16:38: error: expected primary-expression before ‘)’ token
What's the point that I am missing?

You need to create an actual variable to be added to your vector:
int main ()
{
coordinate c;
c.latitude = latitude;
c.longitude = longitude;
previousPoints.push_back (c);

typedef struct
{
std :: string latitude;
std :: string longitude;
} coordinate;
coordinate is typedef on anonymous struct, not object. You should create object of coordinate in your function, or not use typedef, i.e.
struct coord
{
std :: string latitude;
std :: string longitude;
} coordinate;
now, coordinate is object. And one question, why you type spaces after std and after ::? It's legal, but strange.

coordinate.latitude = latitude;
You need an object to access a members of it, but coordinate just names a struct. Also on the right hand side you don't have a value... You need something like:
coordinate c;
c.latitude = "120";
c.longitude = "10";
previousPoints.push_back(c);

Related

Applying callback to call gsl_integration

I am a newbie in c++ and I am trying to modify a code and use gsl_integration library of c in a class called Cosmology. In order to assign member functions to to form a pointer for gsl, I used callback procedure I found by looking up in internet
Update: Cosmology.h
#include <cmath>
#include <gsl/gsl_integration.h>
struct CCallbackHolder
{
Cosmology* cls;
void* data;
};
class Cosmology {
private:
static const double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
double Ez(double z);
double Hz(double z, void* params);
static double CCallback(double z,void* param)
{
CCallbackHolder* h = static_cast<CCallbackHolder*>(param);
return h->cls->Hz(h->data);
}
public:
double distH, timeH;
Cosmology();
Cosmology(double);
Cosmology(double , double );
Cosmology(double , double , double );
Cosmology(double , double , double , double );
Cosmology(double , double , double , double , double );
double distC(double);
} cosmo;
Cosmology.cpp
#include <cmath>
#include <gsl/gsl_integration.h>
#include "Cosmology.h"
#include<iostream>
using namespace std;
double Cosmology::Hz(double z, void* params) {
double result = 1.0/pow(OmegaL + pow(1.0+z,3.0)*OmegaM + pow(1.0+z,4.0)*OmegaG + pow(1.0+z,2.0)*OmegaK, 0.5);
return result;
}
double Cosmology::distC(double z) {
double lower_limit = 0.0, abs_error = 1.0e-8, rel_error = 1.0e-8, alpha = 0.0, result, error;
gsl_integration_workspace *work_ptr = gsl_integration_workspace_alloc(1000);
gsl_function Hz_function;
void* params_ptr = α
Hz_function.function = &Cosmology::CCallback;
Hz_function.params = params_ptr;
gsl_integration_qags(&Hz_function, lower_limit, z, abs_error, rel_error, 1000, work_ptr, &result, &error);
return distH*result;
}
using namespace std;
int main () {
Cosmology cosmo;
cout << "Comoving Distance: " << cosmo.distC (0.3);
return 0;
}
I am getting the following errors when I tried to compile the code:
Cosmology.h:10: error: ISO C++ forbids declaration of ‘Cosmology’ with no type
Cosmology.h:10: error: expected ‘;’ before ‘*’ token
Cosmology.h:16: error: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:16: error: expected ‘;’ before ‘double’
Cosmology.h:17: error: ISO C++ forbids declaration of ‘constexpr’ with no type
Cosmology.h:17: error: expected ‘;’ before ‘double’
In file included from Universe.cpp:3:
Cosmology.h: In static member function ‘static double Cosmology::CCallback(double, void*)’:
by compiling with this line : g++ -Wall -pedantic Cosmology.cpp -o Cosmology std=c++0x. How could I fix the code?
The main problem with your program is that you initialize non-static variables that are class members while declaring them, which is not a legal C++ operation. To be specific, the problem are these lines:
static constexpr double c = 299792458.0, Mpc2Km = 3.08567758e+19, Yrs2Sec = 3.15569e7;
double H0 = 67.77, OmegaM = (0.022161+0.11889)/(H0*H0), OmegaL = 0.6914, OmegaG = 8.24e-5, OmegaK = 0.0009;
c, Mpc2Km, Yrs2Sec, H0, OmegaM, OmegaL, OmegaG, OmegaK are non-static, non-const and double. If you want to initialize them (i.e. assign values to them), you have to do it within the Cosmology class constructor or move them outside of the class declaration and thus make them global, but note that you should avoid global variables unless you know what you do and there's no other way.
If you want to initialize them in the header file like you tried to do, they must be of integral type - won't work for doubles.
Shortly: the first line won't work because the type is double (only integral static const types can be initialized inside class declaration); the second won't work since the type is non-const, non-static and double.
The rest of the errors will probably vanish as soon as these lines are fixed.

Error in C++ : redefinition of class constructor

I've encountered these two error when trying to compile..
anyone knows whats wrong ?
Was thinking maybe I #include the wrong header file ?
the sample of the codes and error as per following:
Error:
Square.cpp:8: error: redefinition of ‘Square::Square(bool, Point*, std::string, int)’
Square.h:21: error: ‘Square::Square(bool, Point*, std::string, int)’ previously defined here
Square.cpp: In member function ‘Point Square::getCoord()’:
Square.cpp:22: error: expected primary-expression before ‘]’ token
Square.cpp: In member function ‘void Square::setCoord(Point*)’:
Square.cpp:32: error: expected primary-expression before ‘]’ token
Square.cpp:32: error: expected primary-expression before ‘]’ token
cpp file
#include "Square.h"`
#include <cmath>
using namespace std;
Square::Square(bool containsWarpSpace, Point coord[], string shapeName, int vertPoint):ShapeTwoD(shapeName, containsWarpSpace) {
vertPoint = vertPoint;
coord[] = coord[];
}
int Square::getVertPoint()
{
return vertPoint;
}
Point Square::getCoord()
{
return coord[];
}
void Square::setVertPoint(int verticleP)
{
vertPoint = verticleP;
}
void Square::setCoord(Point coord[])
{
coord[] = coord[];
}
header:
#include "ShapeTwoD.h"
class Square : public ShapeTwoD
{
private:
int vertPoint;
Point coord[];
public:
//Accessor
int getVertPoint();
Point getCoord();
//Mutator
void setVertPoint(int vertP);
void setCoord(Point coord[]);
//virtual member
virtual double computeArea(Point x, Point y);
Square(bool containsWarpSpace, Point coord[], std::string shapeName = "Square", int vertPoint = 4):ShapeTwoD(shapeName, containsWarpSpace){}
};
You are defining the constructor twice, once in the header and once in the implementation file. In the header, you just need to declare it like this:
Square(bool containsWarpSpace,
Point coord[],
std::string shapeName = "Square",
int vertPoint = 4);
You also need to fix the handling of coord, maybe something like changing coord to
Point* coord;
and use
Point* Square::getCoord()
{
return coord;
}
and
this->coord = coord;
in the constructor and setCoord().
Please note that your way of handling coord seems strange and dangerous to me, but without further information about what you are actually trying to do it's hard to give specific advise. Generally, consider using the standard containers over manual memory/array management.
The compiler clearly tells you the problem:
You defined the constructor twice once in header file and once in cpp file.
Also, What exactly do you intend to do with:
coord[] = coord[];
You should understand each and every statement of code that you write. Think about, What do you intend this statement to do? & then match it to the language grammar that you learnt.
Source File:
Square::Square(bool containsWarpSpace, Point coord[],
string shapeName, int vertPoint)
:ShapeTwoD(shapeName, containsWarpSpace)
{
vertPoint = vertPoint;
coord[] = coord[];
}
Header File:
Square(bool containsWarpSpace, Point coord[],
std::string shapeName = "Square", int vertPoint = 4)
:ShapeTwoD(shapeName, containsWarpSpace)
{}
Looks like two different version of the same function.
The one in the header file calls the base class constructor but does not have any code in the body of the constructor.

c++ Set not compiling properly

I am creating a data structure but when I try and compile I get an error saying that I haven't specified that type of set that I am initializing.
I am working with the NTL library with is used for large numbers.
This is my code:
#include <set>
#include ...
NTL_CLIENT
using namespace std;
using namespace NTL;
const RR ZERO = to_RR(0);
const RR ONE = to_RR(1);
const RR TWO = to_RR(2);
class tenTree
{
public:
tenTree(string newName = "", int newLevel = 0);
~tenTree();
void put(string prefix, RR power);
bool get(string prefix, RR & output);
void display(int depth);
bool isKnown(RR power){return (powers.find(power) != powers.end());};
private:
tenTree* children [10];
set<int> powers;
int level;
string name;
bool child[10];
};
When I try to compile it comes back with an error saying:
twoPow.cpp:47: error: ISO C++ forbids declaration of \u2018set\u2019 with no type
twoPow.cpp:47: error: expected \u2018;\u2019 before \u2018<\u2019 token
twoPow.cpp: In member function \u2018bool tenTree::isKnown(NTL::RR)\u2019:
twoPow.cpp:44: error: \u2018powers\u2019 was not declared in this scope
Is there something that I am missing here?
It was just a matter of the scope. All I had to do was add an std:: before the set and it compiled correctly.

How to fix C++ error: expected unqualified-id

I'm getting this error on line 6:
error: expected unqualified-id before '{' token
I can't tell what's wrong.
#include <iostream>
using namespace std;
class WordGame;
{ // <== error is here on line 6
public:
void setWord( string word )
{
theWord = word;
}
string getWord()
{
return theWord;
}
void displayWord()
{
cout << "Your word is " << getWord() << endl;
}
private:
string theWord;
}
int main()
{
string aWord;
WordGame theGame;
cin >> aWord;
theGame.setWord(aWord);
theGame.displaymessage();
}
There should be no semicolon here:
class WordGame;
...but there should be one at the end of your class definition:
...
private:
string theWord;
}; // <-- Semicolon should be at the end of your class definition
As a side note, consider passing strings in setWord() as const references to avoid excess copying. Also, in displayWord, consider making this a const function to follow const-correctness.
void setWord(const std::string& word) {
theWord = word;
}
Get rid of the semicolon after WordGame.
You really should have discovered this problem when the class was a lot smaller. When you're writing code, you should be compiling about every time you add half a dozen lines.
Semicolon should be at the end of the class definition rather than after the name:
class WordGame
{
};
For what it's worth, I had the same problem but it wasn't because of an extra semicolon, it was because I'd forgotten a semicolon on the previous statement.
My situation was something like
mynamespace::MyObject otherObject
for (const auto& element: otherObject.myVector) {
// execute arbitrary code on element
//...
//...
}
From this code, my compiler kept telling me:
error: expected unqualified-id before for (const auto& element: otherObject.myVector) {
etc...
which I'd taken to mean I'd writtten the for loop wrong. Nope! I'd simply forgotten a ; after declaring otherObject.
For anyone with this situation: I saw this error when I accidentally used my_first_scope::my_second_scope::true in place of simply true, like this:
bool my_var = my_first_scope::my_second_scope::true;
instead of:
bool my_var = true;
This is because I had a macro which caused MY_MACRO(true) to expand into my_first_scope::my_second_scope::true, by mistake, and I was actually calling bool my_var = MY_MACRO(true);.
Here's a quick demo of this type of scoping error:
Program (you can run it online here: https://onlinegdb.com/BkhFBoqUw):
#include <iostream>
#include <cstdio>
namespace my_first_scope
{
namespace my_second_scope
{
} // namespace my_second_scope
} // namespace my_first_scope
int main()
{
printf("Hello World\n");
bool my_var = my_first_scope::my_second_scope::true;
std::cout << my_var << std::endl;
return 0;
}
Output (build error):
main.cpp: In function ‘int main()’:
main.cpp:27:52: error: expected unqualified-id before ‘true’
bool my_var = my_first_scope::my_second_scope::true;
^~~~
Notice the error: error: expected unqualified-id before ‘true’, and where the arrow under the error is pointing. Apparently the "unqualified-id" in my case is the double colon (::) scope operator I have just before true.
When I add in the macro and use it (run this new code here: https://onlinegdb.com/H1eevs58D):
#define MY_MACRO(input) my_first_scope::my_second_scope::input
...
bool my_var = MY_MACRO(true);
I get this new error instead:
main.cpp: In function ‘int main()’:
main.cpp:29:28: error: expected unqualified-id before ‘true’
bool my_var = MY_MACRO(true);
^
main.cpp:16:58: note: in definition of macro ‘MY_MACRO’
#define MY_MACRO(input) my_first_scope::my_second_scope::input
^~~~~
I got this error because I was not declaring a variable and was using it further .
Here is my code why I was getting it.
It was because I was not declaring a variable for size of my >vector.
Just replace
int n=arr.size();
Replace Here,
int sumSubarrayMins(vector<int>& arr) {
int = arr.size();
long long sum;
long long ans =0;
for(long i =0;i<n;i++){
sum =0;
long mini=INT_MAX;
for(long long j =i;j<n;j++){
mini=min(mini,arr[j]);
sum+=mini;
}
ans+=sum;
}
return ans;
}

Trouble compiling a simple C++0x program with lambdas

I am trying to run a simple lambda example.
// lambda.cpp
#include <functional>
//#include <tr1/functional>
int main()
{
// Assign the same lambda expression to a function object.
function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
//function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
}
I'm compiling it like this:
$ g++ -std=c++0x -fpermissive lamdas.cpp
lambdas.cpp: In function ‘int main()’:
lambdas.cpp:10: error: expected primary-expression before ‘=’ token
lambdas.cpp:10: error: expected primary-expression before ‘[’ token
lambdas.cpp:10: error: expected primary-expression before ‘]’ token
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected primary-expression before ‘int’
lambdas.cpp:10: error: expected ‘;’ before ‘{’ token
How do I get it to compile with no errors?
Did you mean std::function?
Standard library features live in the std namespace.
It's also interesting that your copy/paste is clearly fake; you wrote "lamdas.cpp" then compiled "lambdas.cpp"!
std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; };
or, probably better
auto f2 = [] (int x, int y) { return x + y; };
It looks to me like you forgot -std=c++0x.