foo2.cpp:9: error: expected primary-expression before '(' token - c++

What is the error in this file?
I get:
foo2.cpp:9: error: expected primary-expression before '(' token
foo2.cpp:9: error: expected primary-expression before 'int'
foo2.cpp:9: error: expected ';' before '{' token
int main(void)
{
class X {
int i;
public:
X(int ii = 0);
};
X::X(int ii) { i = ii; }
return 0;
}

First, may I ask, WHY?
Second, you can't provide an implementation inside a method (including main). If you must do this, keep the implementation inline:
int main()
{
class X {
int i;
public:
X(int ii = 0){ i = ii; }
};
return 0;
}

You cannot nest functions in C++. To modify your program, you have two alternatives. You may move your class definition outside of main, or you may put your method definition inside the class:
First alternative:
class X {
int i;
public:
X(int ii = 0);
};
X::X(int ii) { i = ii; }
int main(void)
{
return 0;
}
Second alternative:
int main(void)
{
class X {
int i;
public:
X(int ii = 0) { i = ii; }
};
return 0;
}

Related

error: expected type-specifier before 'Mobile'

I did a research but solution is not matching to my program. From the large code i took only erroneous part. Please suggest.
#include <iostream>
using namespace std;
class Products {
static Products* prod;
public:
//Creat a static funtion which does the task what client was doing
static void checkStockStatus(int choice)
{
switch (choice) {
case 1:
prod = new Mobile();
break;
}
prod->inStockStatus();
}
virtual void inStockStatus() = 0;
};
class Mobile : public Products {
public:
void inStockStatus()
{
cout << "Mobiles are instock" << endl;
}
};
int main()
{
int choice = 1;
Products::checkStockStatus(choice);
return 0;
}
OutPut:
In static member function 'static void Products::checkStockStatus(int)':
14:29: error: expected type-specifier before 'Mobile'

stable_sort in C++

Im trying to use stable_sort in order to sort a vector of pointers
to a certain class. I've a code like this :
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class B
{
public :
B(int y, int j) {x = y, r = j;};
void getVal() {cout << x << endl; };
int x;
int r;
};
bool compareB(B* b1, B* b2)
{
return b1->getVal() < b2->getVal();
}
int main()
{
B b1(3, 4), b2(-5, 7), b3(12, 111);
vector<B*> myVec;
myVec.push_back(&b1);
myVec.push_back(&b2);
myVec.push_back(&b3);
std::stable_sort(myVec.begin(), myVec.end(), compareB);
for (size_t size = 0; size < myVec.size(); ++size)
{
myVec[size]->getVal();
}
return 0;
}
However, I get the foolowing error while compiling it :
"error: invalid operands of types 'void' and 'void' to binary 'operator<'
return b1->getVal() < b2->getVal();"
Can someone help me ?
The problem is with
void getVal() {cout << x << endl; };
It returns void instead of some value.
When you use it in return b1->getVal() < b2->getVal(); it boils down to return void < void; which will not compile.
You should be able to change it to
int getVal() { return x; };

C++: Defining class functions (expected primary-expression and expected unqualified id)

I'm new to C++ at university; because the class is going to pick up pace fast, I'm going to implement class functions and header files etc but we're expected to teach ourselves.
I'm getting the following 3 errors:
MagicSquareAlg2.cpp:145:27: error: expected primary-expression before `.' token
diagLength = Diagnostics.userInput(*input, &inputFlag);
^
MagicSquareAlg2.cpp:148:49: error: expected primary-expression before `.' token
vector< vector<int> > magicSquare = MagicSquare.matrixBuild(diagLength);
^
MagicSquareAlg2.cpp:149:13: error: expected unqualified-id before `.' token
Diagnostics.summary(magicSquare, diagLength);
Here is my code. Could someone please offer simple hints as to what I'm doing wrong (enough so that I don't have to cite)? The online tutorials I'm trying haven't helped too much.
class Diagnostics {
public:
vector< vector<int> > magicSquare;
string diagLengthPrep;
bool* inputFlag;
int diagLength;
int userInput(string diagLengthPrep, bool* inputFlag);
void summary(vector< vector<int> > magicSquare, int diagLength);
};
int Diagnostics::userInput(string diagLengthPrep, bool* inputFlag) {
//Code
}
void Diagnostics::summary(vector< vector<int> > magicSquare, int diagLength) {
//Code
}
class MagicSquare {
public:
int diagLength;
vector< vector<int> > matrixBuild(int diagLength);
};
vector< vector<int> > MagicSquare::matrixBuild(int diagLength) {
//Code
}
void session(string* input, bool inputFlag) {
int diagLength;
while (inputFlag == true) {
//Code
diagLength = Diagnostics.userInput(*input, &inputFlag);
}
vector< vector<int> > magicSquare = MagicSquare.matrixBuild(diagLength);
Diagnostics.summary(magicSquare, diagLength);
}
int main() {
//Code
}
You are trying to call non-static member functions without an instance. To fix this, construct instances of your classes and call the functions on them:
void session(string* input, bool inputFlag) {
int diagLength;
Diagnostics diags; //here
while (inputFlag == true) {
//Code
diagLength = diags.userInput(*input, &inputFlag);
}
MagicSquare ms; //here
vector< vector<int> > magicSquare = ms.matrixBuild(diagLength);
diags.summary(magicSquare, diagLength);
}

typedef struct with enum in main() not working

Its practically 4 AM here, I really give up, someone help!
#include <iostream>
using namespace std;
int d;
typedef struct my_data
{
enum calling_func
{
TEST_A,
TEST_B,
TEST_C
} val;
}letter_data;
int main() {
letter_data l;
l.val = TEST_A; // error: 'TEST_A' was not declared in this scope
cout << "test" << endl;
return 0;
}
I'm getting error that TEST_A was not declared in this scope. I'm a beginner with c++ so I admit I'm stupid if not worse ...
Try this:
int main() {
letter_data l;
l.val = my_data::TEST_A; // error: 'TEST_A' was not declared in this scope
cout << "test" << endl;
return 0;
}
By the way, in C++ you don't need the typedef, remove it.
Edit 1: Example without typedef
struct letter_data
{
enum calling_func
{
TEST_A,
TEST_B,
TEST_C
} val;
};
int main() {
letter_data l;
l.val = letter_data::TEST_A; // error: 'TEST_A' was not declared in this scope
cout << "test" << endl;
return 0;
}
Clang produces a very readable error message:
[8:10pm][wlynch#watermelon /tmp] clang++ red.cc
red.cc:21:13: error: use of undeclared identifier 'TEST_A'; did you mean 'my_data::TEST_A'?
l.val = TEST_A; // error: 'TEST_A' was not declared in this scope
^~~~~~
my_data::TEST_A
red.cc:11:9: note: 'my_data::TEST_A' declared here
TEST_A,
^
1 error generated.

Inheritance woes: Using header files and default constructor of the base class in the derived class

I have got a simple question:
Here is the base class:
class CBox
{
public:
double m_Length;
double m_Width;
double m_Height;
CBox()
{
}
CBox(double lv=1.0,double wv=1.0,double hv=1.0)
:m_Length(lv),m_Width(wv),m_Height(hv)
{
}
~CBox();
protected:
private:
};
And here is the derived class:
#ifndef CCANDYBOX_H
#define CCANDYBOX_H
#include "CBox.h"
#include <iostream>
#include <cstring>
class CCandyBox :CBox
{
public:
char* m_Contents;
CCandyBox(double lv, double mv, double hv, char* str):CBox(lv,mv,hv)
{
m_Contents = new char[ strlen(str) +1 ];
for(unsigned int i=0; i< strlen(str)+1; i++)
{ *(m_Contents+i) = *(str +i);
{
*(m_Contents+i) = *(str+i);
}
}
CCandyBox(char* str = "Candy"):CBox()
{
m_Contents = new char[ strlen(str) +1];
for(unsigned int i=0; i<strlen(str)+1;++i)
{
*(m_Contents +i) = *(str+i);
}
}
virtual ~CCandyBox()
{
delete[] m_Contents;
}
CCandyBox(const CCandyBox& other);
CCandyBox& operator=(const CCandyBox& other);
protected:
private:
};
#endif // CCANDYBOX_H
Main.cpp function just includes "CCandyBox.h" and a simple "helloworld"
And gives the following errors:
C:\Work\Check\main.cpp|4|error: expected nested-name-specifier before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected ';' before 'namespace'|
C:\Work\Check\main.cpp|4|error: expected unqualified-id before 'namespace'|
C:\Work\Check\main.cpp|11|error: expected '}' at end of input|
C:\Work\Check\CCandyBox.h||In constructor 'CCandyBox::CCandyBox(double, double, double, char*)':|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before '(' token|
C:\Work\Check\CCandyBox.h|24|error: expected primary-expression before 'char'|
C:\Work\Check\CCandyBox.h|24|error: expected ';' before ':' token|
C:\Work\Check\CCandyBox.h|41|error: expected '}' at end of input|
C:\Work\Check\main.cpp||In member function 'int CCandyBox::main()':|
C:\Work\Check\main.cpp|9|error: 'cout' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\iostream|62|note: 'std::cout'|
C:\Work\Check\main.cpp|9|error: 'endl' was not declared in this scope|
C:\Work\Check\main.cpp|9|note: suggested alternative:|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\ostream|543|note: 'std::endl'|
C:\Work\Check\main.cpp|11|error: expected unqualified-id at end of input|
||=== Build finished: 16 errors, 0 warnings ===|
I am using MinGW with CodeBlocks. Help appreciated
For starters, I'd strongly suggest making life easy on yourself and move all your implementation bodies to separate files (for example, to CBox.cpp and CCandyBox.cpp).
This is wrong:
for(unsigned int i=0; i< strlen(str)+1; i++)
{ *(m_Contents+i) = *(str +i);
{
*(m_Contents+i) = *(str+i);
}
This is better:
for(unsigned int i=0; i < strlen(str); i++)
m_Contents[i] = str[i];
m_Contents[strlen(str)] = '\0';
This is even better:
file CCandyBox.h:
#ifndef CCANDYBOX_H
#define CCANDYBOX_H
#include <string>
#include "CBox.h"
class CCandyBox : public CBox
{
public:
std::string m_Contents;
...
file CCandyBox.cpp:
#include <string.h>
#include "CCandyBox.h"
...
CCandyBox::CCandyBox(double lv, double mv, double hv, char* str)
: CBox(lv,mv,hv)
{
m_Contents = std::string(str);
...