I have the following cpp file, and the error "Declaration expected" is thrown in this line, pointing at "for":
for (int i = 0; i < m_Floats.size(); ++i)
The entire code is:
#include "stdafx.h"
#include <vector>
#include "clsJenksBreaks.h"
using namespace std;
vector<float>m_Floats
vector<float>getJenksBreaks(const unsigned int uNumClass)
{
//std::sort(m_Floats, m_Floats + size, std::greater<float>());
float **mat1 = new float*[m_Floats.size()];
for (int i = 0; i < m_Floats.size(); ++i)
{
mat1[i] = new float[iNumClass];
}
for (unsigned long x=0;x<uNumClass+1;x++)
{
for (unsigned long y=0;y<m_Floats.size()+1,y++)
{
mat1[x][y]=0;
}
}
//I have commented out the other code that is in this function, but the error still exists.
}
Does anybody see where I went wrong?
There is no error on the line you indicate. The errors are:
missing semicolon at the end of line 7 (declaration of m_Floats).
missing declarations of iNumClass and uNumClass (presumably they're in the header you haven't shown us)
comma instead of semicolon on line 20, before the for-loop incrementor.
You're missing a semi-colon after the declaration of m_floats. Try:
vector<float>m_Floats;
Possible typo,
mat1[i] = new float[iNumClass];
should be
mat1[i] = new float[uNumClass];
Related
#include <stdio.h>
void main()
{
int x = 5;
if (x < 1);
printf("Hello");
}
how it output hello
i expected error but there was no eroor
Because you have ';' after the "(x<1)"
Remove that semicolon and it will work.
This question already has answers here:
Code outside functions
(3 answers)
Closed 2 years ago.
#include <iostream>
using namespace std;
int *i = new int;
*i = 0;
int &j = *i;
j++;
//cout << *i << endl;
I have a code like that, and I know this syntax is true but it gives warning in Visual Studio Code like this:
quiz2_q8.cpp:5:4: error: expected constructor, destructor, or type conversion before '=' token
*i = 0;
^
quiz2_q8.cpp:7:1: error: 'j' does not name a type
j++;
Am I missing a library to include? I thought iostream is enough for this quiz code.
You can't have arbitrary statements in the global namespace. You need to put it into a function, e.g. like this:
int main() {
int *i = new int;
*i = 0;
int &j = *i;
j++;
}
Most programs have a starting point, which is the main method/function/procedure whatever you want to call it. Each function has a scope given by { // fun scope }. A good tutorial series on C++ might come to your aid, or perhaps a book. With that said here's a template for such a program.
#include <iostream>
using namespace std;
int main(){
return 0;
}
Statements for being executed must be inside functions.
#include <iostream>
using namespace std;
int main(void) { // add this
int *i = new int;
*i = 0;
int &j = *i;
j++;
//cout << *i << endl;
} // add this
I found this similar problem at
SO: error: ‘Page’ was not declared in this scope
but it is a different situation since I'm not working on header files.
As you can see from the code below, I'm using PoDoFo library trying to print out a pdf file content to the screen
#include <iostream>
#include <podofo/podofo.h>
using namespace std;
int main()
{
//load a new document
//PoDoFo::PdfMemDocument pdf("myDoc.pdf");
// this load a doc on my disk
PoDoFo::PdfMemDocument doc;
doc.Load("myDoc.pdf");
//iterate over each page"
for(int pn = 0; pn < doc.GetPageCount(); ++pn){
PoDoFo::PdfPage* page = doc.GetPage(pn);
}
//
PoDoFo::PdfContentsTokenizer tok(page);
const char* token = nullptr;
PoDoFo::PdfVariant var;
PoDoFo::EPdfContentsType type;
while (tok.ReadNext(type, token, var)) {
if (type == PoDoFo::ePdfContentsType_Keyword) {
}
}
if (var.IsArray()) {
PoDoFo::PdfArray& a = var.GetArray();
for (size_t i = 0; i < a.GetSize(); ++i)
if (a[i].IsString()) { }
}
}
This is the error:
/home/coder/QtProjects/finalProject/main.cpp:19: error: ‘page’ was not declared in this scope
PoDoFo::PdfContentsTokenizer tok(page);
hope you can help me fix this.
Thanks!
I suspect your curly braces aren't in the correct place, as per #Scheff's comment the scope of the page variable is contained within the for loop and you are attempting to do more operations after the loop is over. I moved the } for the for loop where I think it probably should be in the code below and it should work, although I also suspect the closing } of the while loop and last if might also be in the wrong spot.
#include <iostream>
#include <podofo/podofo.h>
using namespace std;
int main()
{
//load a new document
//PoDoFo::PdfMemDocument pdf("myDoc.pdf");
// this load a doc on my disk
PoDoFo::PdfMemDocument doc;
doc.Load("myDoc.pdf");
PoDoFo::PdfPage* page
//iterate over each page"
for(int pn = 0; pn < doc.GetPageCount(); ++pn){
PoDoFo::PdfPage* page = doc.GetPage(pn);
//
PoDoFo::PdfContentsTokenizer tok(page);
const char* token = nullptr;
PoDoFo::PdfVariant var;
PoDoFo::EPdfContentsType type;
while (tok.ReadNext(type, token, var)) {
if (type == PoDoFo::ePdfContentsType_Keyword) {
}
}
if (var.IsArray()) {
PoDoFo::PdfArray& a = var.GetArray();
for (size_t i = 0; i < a.GetSize(); ++i)
if (a[i].IsString()) { }
}
}
}
error: expected primary-expression before â)â token
I'm not entirely sure what's going on here, since my friends also working on this project can't seem to tell what's wrong. Any help on this error would be appreciated. The line that the error is referring to has a comment on it pointing it out. I'm trying to insert a pair into a map by the code below.
theCandidates is a map<string, class>, and in this case, that class is called Candidate.
void TallyVotes::initialize(Scanner& inStream)
{
numberOfLosers = 0;
numberOfVotes = boost::lexical_cast<int>(inStream.next());
numberOfCandidates = boost::lexical_cast<int>(inStream.next());
for(int i = 0; i < numberOfVotes ; i++)
{
for(int j = 0; j < numberOfCandidates ; i++)
{
theVotes[i][j] = inStream.next();
cand = theVotes[i][j];
if(i == 0)
{
theCandidates.insert(make_pair(cand, Candidate));//ERROR ON THIS LINE
}
}
}
} // void TallyVotes::initialize(Scanner& inStream)
The make_pair function takes two values as arguments, not a value and a type.
Try e.g.
make_pair(cand, Candidate())
// Note parentheses ^^
The expression Candidate() create a temporary object, which is then copied into the std::map.
So I have a class called HPStack and I have to include it in my main class etc. However I get a "In File included from" error, what could be causing this?
Also my string objects also have errors I have have no idea why, the error is: "Unable to identifier string".
I'm new the C++ so any help would be appreciated, thanks in advance.
The error I am getting (I think) are these:
error: expected unqualified-id before "namespace"
error: expected `,' or `;' before "namespace"
error: expected namespace-name before ';' token
error: `<type error>' is not a namespace
Im not sure what I am missing but that isn't telling me much.
Here is my code: The class.h file.
#ifndef HPSTACK_H
#define HPSTACK_H
class HPStack {
public:
HPStack();
void push(double);
double pop();
double peek();
private:
double register_[4];
}
#endif
The class.cpp file.
#include "HPStack.h"
#include <cstdlib>
HPStack::HPStack() : register_{}{
}
double HPStack::push(double x) {
for (int i = 2; i >= 0; i--) {
if (isdigit(register_[i])) {
register_[i] = register_[i + 1];
}
register_[0] = x;
}
}
double HPStack::pop() {
return register_[0];
for (int i = 0; i < 3; i++) {
register_[i] = register_[i + 1];
}
}
double HPStack::peek() {
return register_[0];
}
And my main file:
#include "HPStack.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
} else if (token == "+") {
double x = stack.pop();
double y = stack.pop();
double z = (y + x);
stack.push(z);
}
}
cout << stack.peek();
}
The error is, I'm guessing, because of this line:
double register_[4] = {};
You can not initialize class members when declaring them.
If your compiler is new enough to support C++11 features, you can use an initializer list with the constructor:
HPStack::HPStack()
: register_{}
{
}
Otherwise you have to initialize the array manually in the constructor.
And as I noted in a comment, using register_ - 2 makes no sense as it returns a pointer so the index variable i will be way beyond the end of the array.
And using register_ - 1 as the condition in the pop loop makes even less sense, as it will always be non-zero and therefore always true and the loop will loop forever.
You're missing the ; at the end of the class definition:
class HPStack {
...
}; // <== This semicolon is required