I have this code:
FILE *f = fopen(intPath, "r");
Node *n;
if (f) {
try {
n = parse(f, intPath);
} catch (SyntaxError e) {
fclose(f); /***** line 536 *****/
throw LangException(
builtin_classes::exception_class::create_ImportError(
String::fromAscii(e.filename)->
append(String::fromAscii(":"))->
append(String::fromInt(e.line))->
append(String::fromAscii(":"))->
append(String::fromInt(e.col))->
append(String::fromAscii(": syntax error: "))->
append(String::fromAscii(e.message))
);
}
fclose(f);
return n->eval(scope);
} else {
throw LangException(
builtin_classes::exception_class::create_ImportError(
String::fromAscii("failed to open file for reading")
),
line,
col
);
}
And the compiler gives this error:
nodes.cpp:537:40: error: expected primary-expression before
‘(’token
nodes.cpp:544:94: error: expected‘)’before‘;’token
I have no clue what it could be, especially since that code sample has another statement which does the same thing, and it doesn’t cause an error.
“Expected primary-expression before ‘some‘ token” is one of the most common errors that you can experience in Arduino code. Arduino code is written in C++ with few additions here and there, so it is a C++ syntax error. There are multiple versions of this error, depends on what is it that you messed up. Some are easy to fix, some not so much.
Most of the times (but not always), the error occurs because you have missed something or put it at the wrong place. Be it a semicolon, a bracket or something else. It can be fixed by figuring out what is that you missed/misplaced and placing it at the right position. Let us walk through multiple versions of the error and how to fix them one by one.
We all like building things, don’t we? Arduino gives us the opportunity to do amazing things with electronics with simply a little bit of code. It is an open-source electronics platform. It is based on hardware and software which are easy to learn and use. If I were to explain in simple language what Arduino does – it takes an input from the user in different forms such as touch or light and turns it into an output such as running a motor. Actually, you can even post tweets on Twitter with Arduino.
Table of Contents
- How to fix “Expected Primary-Expression Before” error?
- Type 1: Expected primary-expression before ‘}’ token
- Type 2: Expected primary expression before ‘)’ token
- Type 3: Expected primary-expression before ‘enum’
- Type 4: Expected primary expression before ‘.’
- Type 5: Expected primary-expression before ‘word’
- Type 6: Expected primary-expression before ‘else’
- Conclusion
I’ll walk you through multiple examples of where the error can occur and how to possibly fix it. The codes that I use as examples in this article are codes that people posted on forums asking for a solution, so all credits of the code go to them. Let’s begin.
Type 1: Expected primary-expression before ‘}’ token
This error occurs when when the opening curly brackets ‘{‘ are not properly followed by the closing curly bracket ‘}’. To fix this, what you have to do is: check if all of your opening and closing curly brackets match properly. Also, check if you are missing any curly brackets. There isn’t much to this, so I’ll move on to the other types.
Type 2: Expected primary expression before ‘)’ token
Example 1: All credits to this thread. Throughout all of my examples, I will highlight the line which is causing the issue with red.
#include <Adafruit_NeoPixel.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#define PIN D1
#define NUMPIXELS 597
int red = 0;
int green = 0;
int blue = 0;
int game = 0;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Blynk.begin("d410a13b55560fbdfb3df5fe2a2ff5", "8", "12345670");
pixels.begin();
pixels.show();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V1) {
game = 1;
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
setSome(R, G, B);
}
BLYNK_WRITE(V2) {
if (param.asInt()==1) {
game = 2;
rainbow(uint8_t); // Rainbow
}
else {
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
Blynk.run();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i+j) & 255));
}
pixels.show();
delay(wait);
}
// delay(1);
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}BLYNK_WRITE(V3) {
if (param.asInt()) {
game = 3;
setAll(125, 47, 0); //candle
}
else {
}
}
BLYNK_WRITE(V4) {
game = 4;
int Bright = param.asInt();
pixels.setBrightness(Bright);
pixels.show();
}
BLYNK_WRITE(V5) {
if (param.asInt()) {
game = 5;
setAll(85, 0, 255);
}
else {
}
}
BLYNK_WRITE(V6) {
if (param.asInt()) {
game = 6;
oFF(red, green, blue);
// fullOff();
}
else {
}
}
BLYNK_WRITE(V7) {
if (param.asInt()) {
game = 7;
setAll(255, 0, 85);
}
else {
}
}
BLYNK_WRITE(V8) {
if (param.asInt()) {
game = 8;
setAll(90, 90, 90);
}
else {
}
}
BLYNK_WRITE(V9) {
if (param.asInt()) {
game = 9;
setAll(255, 130, 130);
}
else {
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void oFF(byte r, byte g, byte b) {
if (game == 1) {
offsome(r, g, b);
}
else if (game == 2) {
offall(r, g, b);
}
else if (game == 3) {
offall(r, g, b);
}
else if (game == 4) {
offall(r, g, b);
}
else if (game == 5) {
offall(r, g, b);
}
else if (game == 6) {
offall(r, g, b);
}
else if (game == 7) {
offall(r, g, b);
}
else if (game == 8) {
offall(r, g, b);
}
else if (game == 9) {
offall(r, g, b);
}
}
void offall(byte r, byte g, byte b) {
uint32_t x = r, y = g, z = b;
for (x; x > 0; x--) {
if( y > 0 )
y--;
if( z > 0 )
z--;
for(int i = 0; i < NUMPIXELS; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
pixels.show();
delay(0);
}
//delay(0);
}
void offsome(byte r, byte g, byte b) {
uint32_t x = r, y = g, z = b;
for (x; x > 0; x--) {
if( y > 0 )
y--;
if( z > 0 )
z--;
for(int i = 87; i < 214; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
for(int i = 385; i < 510; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
pixels.show();
delay(0);
}
}
void setAll(byte r, byte g, byte b) {
uint16_t x = 0, y = 0, z = 0;
for (x; x < r; x++) {
if( y < g )
y++;
if( z < b )
z++;
for(int i = 0; i < NUMPIXELS; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
pixels.show();
red = r;
green = g;
blue = b;
delay(0);
}
//delay(0);
}
void setSome(byte r, byte g, byte b) {
uint16_t x = 0, y = 0, z = 0;
for (x; x < r; x++) {
if( y < g )
y++;
if( z < b )
z++;
for(int i = 86; i < 212; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
for(int i = 385; i < 512; i++ ) {
pixels.setPixelColor(i, pixels.Color(x, y, z));
}
pixels.show();
red = r;
green = g;
blue = b;
delay(0);
}
//delay(0);
}
void fullOff() {
for(int i = 0; i < NUMPIXELS; i++ ) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
pixels.show();
}
Solution 1:
The error occurs in this code because the rainbow function is supposed to have a variable as its argument, however the argument given here is ‘uint8_t’ which is not a variable.
BLYNK_WRITE(V2) {
if (param.asInt()==1) {
game = 2;
rainbow(uint8_t); // Rainbow
}
else {
}
}
Here all you have to do is define uint8_t as a variable first and assign it a value. The code will work after that.
Type 3: Expected primary-expression before ‘enum’
Example 1: All credits to this thread.
#include <iostream>
using namespace std;
int main()
{
enum userchoice
{
Toyota = 1,
Lamborghini,
Ferrari,
Holden,
Range Rover
};
enum quizlevels
{
Hardquestions = 1,
Mediumquestions,
Easyquestions
};
return 0;
}
Solution 1:
The “expected primary-expression before ‘enum’ ” error occurs here because the enum here has been defined inside a method, which is incorrect. The corrected code is:
#include <iostream>
using namespace std;
enum userchoice
{
Toyota = 1,
Lamborghini,
Ferrari,
Holden,
RangeRover
};
enum quizlevels
{
HardQuestions = 1,
MediumQuestions,
EasyQuestions
};
int main()
{
return 0;
}
Note: Another mistake has been fixed in this code i.e. the space in “Range Rover” variable. Variable names cannot contain spaces.
Type 4: Expected primary expression before ‘.’
Example 1: All credits go to this thread.
#include <iostream>
using std::cout;
using std::endl;
class square {
public:
double length, width;
square(double length, double width);
square();
~square();
double perimeter();
};
double square::perimeter() {
return 2*square.length + 2*square.width;
}
int main() {
square sq(4.0, 4.0);
cout << sq.perimeter() << endl;
return 0;
}
Solution 1: Here the error occurs because “square” is being used as an object, which it is not. Square is a type, and the corrected code is given below.
#include <iostream>
using std::cout;
using std::endl;
class square {
public:
double length, width;
square(double length, double width);
square();
~square();
double perimeter();
};
double square::perimeter() {
return 2*length + 2*width;
}
int main() {
square sq(4.0, 4.0);
cout << sq.perimeter() << endl;
return 0;
}
Type 5: Expected primary-expression before ‘word’
Example 1: All credits go to this thread.
#include <iostream>
#include <string>
using namespace std;
string userInput();
int wordLengthFunction(string word);
int permutation(int wordLength);
int main()
{
string word = userInput();
int wordLength = wordLengthFunction(string word);
cout << word << " has " << permutation(wordLength) << " permutations." << endl;
return 0;
}
string userInput()
{
string word;
cout << "Please enter a word: ";
cin >> word;
return word;
}
int wordLengthFunction(string word)
{
int wordLength;
wordLength = word.length();
return wordLength;
}
int permutation(int wordLength)
{
if (wordLength == 1)
{
return wordLength;
}
else
{
return wordLength * permutation(wordLength - 1);
}
}
Solution 1:
Here, they are incorrectly using string inside wordLengthFunction().
Fixing it is simple, simply replace
int wordLength = wordLengthFunction(string word);
by
int wordLength = wordLengthFunction(word);
Type 6: Expected primary-expression before ‘else’
Example 1: All credit goes to this thread.
// Items for sale:
// Gizmos - Product number 0-999
// Widgets - Product number 1000-1999
// doohickeys - Product number 2000-2999
// thingamajigs - Product number 3000-3999
// Product number >3999 = Invalid Item
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float ProdNumb; // Product Number
double PrG; // Product Number for Gizmo
double NG; // Number of items
double PG; // Price of Item
double PrW; // Product Number for Widgets
double NW; // Number of items
double PW; // Price of Item
double PrD; // Product Number for Doohickeys
double ND ; // Number of items
double PD ; // Price of Item
double PrT; // Product Number for Thingamajigs
double NT; // Number of items
double PT; // Price of Item
double PrI; //Product Number for Invalid (> 3999)
double NI; // Number of items
double PI; // Price of Item
double total = 0;
int main ()
{
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
while (ProdNumb != -1)
{
if (ProdNumb >= 0 && ProdNumb <= 999)
{
ProdNumb == PrG;
cout << "Enter the number of items sold: ";
cin >> NG;
cout << "Enter the price of one of the items sold: ";
cin >> PG;
}
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
else (ProdNumb >= 1000 && ProdNumb <= 1999)
{
ProdNumb == PrW;
cout << "Enter the number of items sold: ";
cin >> NW;
cout << "Enter the price of one of the items sold: ";
cin >> PW;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
}
else (ProdNumb >= 2000 && ProdNumb <= 2999)
{
ProdNumb == PrD;
cout << "Enter the number of items sold: ";
cin >> ND;
cout << "Enter the price of one of the items sold: ";
cin >> PD;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
}
else (ProdNumb >= 3000 && ProdNumb <= 3999)
{
ProdNumb == PrT;
cout << "Enter the number of items sold: ";
cin >> NT;
cout << "Enter the price of one of the items sold: ";
cin >> PT;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
}
else (ProdNumb <= -2 && ProdNumb == 0 && ProdNumb >= 4000)
{
ProdNumb == PrI;
cout << "Enter the number of items sold: ";
cin >> NI;
cout << "Enter the price of one of the items sold: ";
cin >> PI;
cout << "Enter the product number of the item sold: ";
cin >> ProdNumb;
}
}
cout << "***** Product Sales Summary *****";
cout << "n";
cout << "n";
cout << "Gizmo Count: ";
total += NG;
cout << NG;
cout << "n";
cout << "Gizmo Sales Total: ";
cout << (NG)*(PG);
cout << "n";
cout << "n";
cout << "Widget Count: ";
total += NW;
cout << NW;
cout << "n";
cout << "Widget Sales Total: ";
cout << (NW)*(PW);
cout << "n";
cout << "n";
cout << "Dookickey Count: ";
total += ND;
cout << ND;
cout << "n";
cout << "Doohickey Sales Total: ";
cout << (ND)*(PD);
cout << "n";
cout << "n";
cout << "Thingamajig Count: ";
total += NT;
cout << NT;
cout << "n";
cout << "Thingamajig Sales Total: ";
cout << (NT)*(PT);
cout << "n";
cout << "n";
cout << "Invalid Sales: ";
total += NI;
cout << NI;
return 0;
}
Solution 1:
This code is not correct because after the if statement is closed with ‘}’ in this code, there are two statements before the else statement starts. There must not be any statements between the closing curly bracket ‘}’ of if statement and the else statement. It can be fixed by simply removing the part that I have marked in red.
Conclusion
And that’s it, I hope you were able to fix the expected primary-expression before error. This article wasn’t easy to write – I’m in no way an expert in C++, but I do know it to a decent level. I couldn’t find any articles related to fixing this error on the internet so I thought I’d write one myself. Answers that I read in forums helped me immensely while researching for this article and I’m thankful to the amazing community of programmers that we have built! If you would like to ask me anything, suggest any changes to this article or simply would like to write for us/collaborate with us, visit our Contact page. Thank you for reading, I hope you have an amazing day.
Also, tell me which one of the 6 types were you experiencing in the comments below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
#include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 30 typedef struct User_t { char *login; char *password; char *name; char *sex; int age; int id; float height; float weight; }User; /*+*/void clean_memory(User** users, unsigned size) { int i; for(i=0;i<size;i++) { free((*users)[size].login); free((*users)[size].password); free((*users)[size].name); free((*users)[size].sex); } free(*users); *users = NULL; } /*+*/void output_f(User** users) { int value,n,i; printf ("1 - output single data"); printf ("2 - output all data"); printf ("select an action : n"); scanf ("%d",&n); switch(n) { case 1: printf ("Input user's id"); scanf ("%d",value); printf ("{id: %d, login: "%s", password: "%s"}n{ name: "%s", sex: "%s", age: %d}n { height:%f, weight:%f}n", (*users)[value].id,(*users)[value].login,(*users)[value].password,(*users)[value].name,(*users)[value].sex, (*users)[value].age,(*users)[value].height,(*users)[value].weight); break; case 2: for(i=0;i<value;i++) printf("{id: %d, login: "%s", password: "%s"}n{ name: "%s", sex: "%s", age: %d}n { height:%f, weight:%f}n", (*users)[value].id, (*users)[value].login, (*users)[value].password,(*users)[value].name,(*users)[value].sex, (*users)[value].age,(*users)[value].height,(*users)[value].weight); break; default : break; } } /*+*/void include_f(User** users, unsigned size) { char buffer[128]; bool sex; printf("Input user's loginn"); scanf("%127s", buffer); (users*)[size].login = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[size].login, buffer); printf("Input user's passwordn"); scanf("%127s", buffer); (users*)[size].password = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[size].password, buffer); printf("Input user's namen"); scanf("%127s", buffer); (users*)[size].name = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[size].name, buffer); printf("Input user's sex(0-female or 1-male)n"); scanf("%d",sex); switch(sex) { case 0: (users*)[size].sex=(char*) malloc(strlen("female")+1); strcpy((users*)[size].sex, buffer); break; case 1: (users*)[size].sex=(char*) malloc(strlen("male")+1); strcpy((users*)[size].sex, buffer); break; default: break; } printf("Input user's agen"); scanf("%d",(users*)[size].age); printf("Input user's heightn"); scanf("%f",(users*)[size].height); printf("Input user's weightn"); scanf("%f",(users*)[size].weight); } /*+*/void delete_f(User** users) { int value; printf("Input user's id"); scanf("%d",value); free((*users)[value].login); free((*users)[value].password); free((*users)[value].name); free((*users)[value].sex); users[value] = NULL; } void search_f(User** users) { } void sort_f(User** users) { int value; printf("set the sort task:"); printf("1 - age n"); printf("2 - height n"); printf("3 - weight n"); printf("4 - sex (male->female) n"); printf("5 - ABC(A->Z) n"); scanf("%d",value); switch(value) { case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; default : printf("input right valuen"); break; } } /*+*/void edit_f(User** users) { int value; bool sex; char buffer[128]; printf ("Input user's id"); scanf ("%d",value); printf ("Input user's loginn"); scanf("%127s", buffer); (users*)[value].login = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[value].login, buffer); printf("Input user's passwordn"); scanf("%127s", buffer); (users*)[value].password = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[value].password, buffer); printf("Input user's namen"); scanf("%127s", buffer); (users*)[value].name = (char*) malloc(strlen(buffer) + 1); strcpy((users*)[value].name, buffer); printf("Input user's sex(0-female or 1-male)n"); scanf("%d",sex); switch(sex) { case 0: (users*)[value].sex=(char*) malloc(strlen("female")+1); strcpy((users*)[value].sex, "female"); break; case 1: (users*)[value].sex=(char*) malloc(strlen("male")+1); strcpy((users*)[value].sex, "male"); break; default: break; } printf("Input user's agen"); scanf("%d",(users*)[value].age); printf("Input user's heightn"); scanf("%f",(users*)[value].height); printf("Input user's weightn"); scanf("%f",(users*)[value].weight); } /*+*/void input_f(User** users,unsigned size) { unsigned i; char buffer[128]; bool sex; for(i=0;i<size;i++) { (*users)->id=i; printf("Input user's #%dnlogin",i); scanf("%127s", buffer); (*users)[i].login = (char*) malloc(strlen(buffer) + 1); strcpy((*users)[i].login, buffer); printf("Input user's passwordn"); scanf("%127s", buffer); (*users)[i].password = (char*) malloc(strlen(buffer) + 1); strcpy((*users)[i].password, buffer); printf("Input user's nnamen"); scanf("%127s", buffer); (*users)[i].name = (char*) malloc(strlen(buffer) + 1); strcpy((*users)[i].name, buffer); printf("Input user's sex(0-female or 1-male)n"); scanf("%d",sex); switch(sex) { case 0: (*users)[i].sex=(char*) malloc(strlen("female")+1); strcpy((*users)[i].sex, "female"); break; case 1: (*users)[i].sex=(char*) malloc(strlen("male")+1); strcpy((*users)[i].sex, "male"); break; default: break; } printf("Input user's agen"); scanf("%d",(*users)[i].age); printf("Input user's heightn"); scanf("%f",(*users)[i].height); printf("Input user's weightn"); scanf("%f",(*users)[i].weight); } } int main () { User *users = NULL; unsigned size,value; bool program=true; printf("Input number of usersn"); scanf("%d",&size); size = size <= MAX_SIZE? size: MAX_SIZE; users = (User*) malloc((size+3) * sizeof(User)); /*+*/printf("1 - inputn"); /*+*/printf("2 - outputn"); /*+*/printf("3 - include (from the end )n"); /*+*/printf("4 - deleten"); printf("5 - searchn"); printf("6 - sortn"); /*+*/printf("7 - editn"); /*+*/printf("8 - exitn"); while (program) { printf("select an action : n"); scanf("%d", value); switch(value) { case 1: input_f(&users,size); break; case 2: output_f(&users); break; case 3: include_f(&users,size); size++; break; case 4: delete_f(&users); break; case 5: search_f(&users); break; case 6: sort_f(&users); break; case 7: edit_f(&users); break; case 8: program=false; break; default: printf("input right valuen"); break; } } clean_memory(&users,size); return 0; } |
<The previous article in this series | The table of contents of this series | The next article in this series>
The error message seems not correct, and the underlying rule seems not reasonable. . . . Anyway, let me solve the problem.
Topics
About:
C++
The table of contents of this article
- Starting Context
- Target Context
- Orientation
- Main Body
- 1: Meeting an «expected primary-expression before ‘>’ token» C++ GCC Compile Error
- 2: Trying to Solve the Compile Error
- 3: Solving the Compile Error, and Some Complaints
Starting Context
- The reader has a basic knowledge on C++.
Target Context
- The reader will know how to solve an «expected primary-expression before ‘» «>» or whatever «‘ token» C++ GCC compile error.
Orientation
There is an article that clarifies the concept of template in C++.
There is an article that clarifies the distinction between definition and declaration in C++.
Main Body
1: Meeting an «expected primary-expression before ‘>’ token» C++ GCC Compile Error
Hypothesizer 7
«expected primary-expression before ‘>’ token»? What is that supposed to mean? . . .
Well, I am trying to compile this piece of C++ code with GCC (note that I put function template body declarations (which should not be called «definitions») into ‘tpp’ files).
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.hpp
@C++ Source Code
#ifndef __theBiasPlanet_coreUtilitiesTests_templatesTest1_ClassE_hpp__#define __theBiasPlanet_coreUtilitiesTests_templatesTest1_ClassE_hpp__namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {class ClassE {public:ClassE ();virtual ~ClassE ();template <typename U> U methodE0 (U const & a_argument0) const;};}}}#endif
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.cpp
@C++ Source Code
#include "theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.hpp"namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {ClassE::ClassE () {}ClassE::~ClassE () {}}}}
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.tpp
@C++ Source Code
#include "theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.hpp"namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {template <typename U> U ClassE::methodE0 (U const & a_argument0) const {return a_argument0;}}}}
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassF.hpp
@C++ Source Code
#ifndef __theBiasPlanet_coreUtilitiesTests_templatesTest1_ClassF_hpp__#define __theBiasPlanet_coreUtilitiesTests_templatesTest1_ClassF_hpp__#include "theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassE.hpp"namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {class ClassF {public:template <typename U> static void methodF0 (U const & a_argument0);static void methodF1 (ClassE const & a_argument0);};}}}#endif
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassF.cpp
@C++ Source Code
#include "theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassF.hpp"#include <string>namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {void ClassF::methodF1 (ClassE const & a_argument0) {a_argument0.methodE0 <std::string> ("aaa");}}}}
theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassF.tpp
@C++ Source Code
#include "theBiasPlanet/coreUtilitiesTests/templatesTest1/ClassF.hpp"#include <string>namespace theBiasPlanet {namespace coreUtilitiesTests {namespace templatesTest1 {template <typename U> void ClassF::methodF0 (U const & a_argument0) {a_argument0.methodE0 <std::string> ("aaa");}}}}
The ‘>’ token mentioned in the message points to the ‘>’ of ‘a_argument0.methodE0 <std::string> («aaa»);’.
In the first place, what, the hell, is «primary-expression»?
This page claims that it means a building block of more complex expressions. . . . I do not know . . .
I do not know because then, I cannot imagine any expression that is not a primary-expression. In fact, what is an expression that cannot be any building block of any more complex expression? . . . For example, an expression, ‘1 + 2’, can be a building block of ‘1 + 2 + 3’; so it should be a primary-expression, although the page does not seem to admit ‘1 + 2’ to be a primary-expression . . .
Anyway, I thank that the page explicitly states that any class name like «MyClass» and «A::B» or any «template id» like «A<int>» is a primary-expression. So, ‘std::string’ is definitely a primary-expression, is it not? . . . The some other documents I have found seem to agree that it is really a primary-expression.
Well, the thing that is before the ‘>’ in my code is . . . «std::string», an assured primary-expression. . . . Mr. compiler, a primary-expression seems to be already there . . .
2: Trying to Solve the Compile Error
Hypothesizer 7
The sloppiness of the message aside, that message was a reminiscent of my past struggle with template.
At that time, I had to put ‘typename’ in order to explicitly teach the compiler that a type name was a type name, which was for assuring that the ‘<‘-‘>’ pair was for template parameterization, not for comparing things.
Well, does the compiler require ‘typename’? How about this?
@C++ Source Code
a_argument0.methodE0 <typename std::string> ("aaa");"
Nope.
Why? The compiler should now know that the ‘>’ is not any comparison operator, should not?
3: Solving the Compile Error, and Some Complaints
Hypothesizer 7
In fact, there seems to be a rule that any template member on any template-type dependent qualification has to be explicitly declared as a template with ‘.template’.
Well, in my case, ‘methodE0 <U>’ is a template member of ‘a_argument0’, which is a template-type dependent qualification (certainly, it is the qualification and dependent on the template type, ‘U’), so, it has to be ‘a_argument0.template methodE0 <std::string> («aaa»)’.
Hmm . . . , certainly, that has solved the problem, but is such a rule reasonable? . . . I mean, I understand that the rule exists, but I do not understand why the rule is not foolish.
You know, I am not any person that is happy just because a problem has been solved without any satisfactory explanation; I want an explanation why such a rule is necessary.
For example, this (which already exists in the above code) does not cause the error.
@C++ Source Code
void ClassF::methodF1 (ClassE const & a_argument0) {a_argument0.methodE0 <std::string> ("aaa");}
That is because, of course, the qualification is independent of any template type, escaping the premise of the rule.
However, I do not see any reason why the flagged line is more a difficulty than the passed line is, for the compiler. . . . You know, the template itself is never compiled, but instantiations of the template are compiled, right? In fact, ‘void ClassF::methodF0 <ClassE> (ClassE const & a_argument0)’ should be no different from ‘void ClassF::methodF1 (ClassE const & a_argument0)’ except the method name.
I understand that a rule that disambiguates ‘<‘ and ‘>’ may be required, but I doubt that the present rule is wisely chosen.
If there is a reasonable necessity for the rule to exist, an understandable explanation would be nice. If the explanation is «You must just obey because I said so!», I would regard such a rule tyrannical, and foolish.
References
<The previous article in this series | The table of contents of this series | The next article in this series>
The expected primary expression before occurs due to syntax errors. It usually has a character or a keyword at the end that clarifies the cause. Here you’ll get access to the most common syntax mistakes that throw the same error.
Continue reading to see where you might be getting wrong and how you can solve the issue.
Contents
- Why Does the Expected Primary Expression Before Occur?
- – You Are Specifying the Data Type With Function Argument
- – The Wrong Type of Arguments
- – Issue With the Curly Braces Resulting in Expected Primary Expression Before }
- – The Parenthesis Following the If Statement Don’t Contain an Expression
- How To Fix the Given Error?
- – Remove the Data Type That Precedes the Function Argument
- – Pass the Arguments of the Expected Data Type
- – Ensure The Equal Number of Opening and Closing Curly Brackets
- – Add an Expression in the If Statement Parenthesis
- FAQ
- – What Does It Mean When the Error Says “Expected Primary Expression Before Int” in C?
- – What Is a Primary Expression in C Language?
- – What Are the Types of Expressions?
- Conclusion
Why Does the Expected Primary Expression Before Occur?
The expected primary expression before error occurs when your code doesn’t follow the correct syntax. The mistakes pointed out below are the ones that often take place when you are new to programming. However, a programmer in hurry might make the same mistakes.
So, here you go:
– You Are Specifying the Data Type With Function Argument
If your function call contains the data type along with the argument, then you’ll get an error.
Here is the problematic function call:
int addFunction(int num1, int num2)
{
int sum;
sum = num1 + num2;
return sum;
}
int main()
{
int result = addFunction(int 20, int 30);
}
– The Wrong Type of Arguments
Passing the wrong types of arguments can result in the same error. You can not pass a string to a function that accepts an argument of int data type.
int main()
{
int result = addFunction(string “cat”, string “kitten”);
}
– Issue With the Curly Braces Resulting in Expected Primary Expression Before }
Missing a curly bracket or adding an extra curly bracket usually results in the mentioned error.
– The Parenthesis Following the If Statement Don’t Contain an Expression
If the parenthesis in front of the if statement doesn’t contain an expression or the result of an expression, then the code won’t run properly. Consequently, you’ll get the stated error.
How To Fix the Given Error?
You can fix the “expected primary-expression before” error by using the solutions given below:
– Remove the Data Type That Precedes the Function Argument
Remove the data type from the parenthesis while calling a function to solve the error. Here is the correct way to call a function:
int main()
{
int result = addFunction(30, 90);
}
– Pass the Arguments of the Expected Data Type
Double-check the function definition and pass the arguments of the type that matches the data type of the parameters. It will ensure that you pass the correct arguments and kick away the error.
– Ensure The Equal Number of Opening and Closing Curly Brackets
Your program must contain an equal number of opening and closing curly brackets. Begin with carefully observing your code to see where you are doing the mistake.
– Add an Expression in the If Statement Parenthesis
The data inside the parenthesis following the if statement should be either an expression or the result of an expression. Even adding either true or false will solve the issue and eliminate the error.
FAQ
You can view latest topics and suggested topics that’ll help you as a new programmer.
– What Does It Mean When the Error Says “Expected Primary Expression Before Int” in C?
The “expected primary expression before int” error means that you are trying to declare a variable of int data type in the wrong location. It mostly happens when you forget to terminate the previous statement and proceed with declaring another variable.
– What Is a Primary Expression in C Language?
A primary expression is the basic element of a complex expression. The identifiers, literals, constants, names, etc are considered primary expressions in the C programming language.
– What Are the Types of Expressions?
The different types of expressions include arithmetic, character, and logical or relational expressions. An arithmetic expression returns an arithmetic value. A character expression gives back a character value. Similarly, a logical value will be the output of a logical or relational expression.
Conclusion
The above error revolves around syntax mistakes and can be solved easily with a little code investigation. The noteworthy points depicting the solutions have been written below to help you out in removing the error:
- Never mention the data type of parameters while calling a function
- Ensure that you pass the correct type of arguments to the given function
- You should not miss a curly bracket or add an extra one
- The if statement should always be used with the expressions, expression results, true, or false
The more you learn the syntax and practice coding, the more easily you’ll be able to solve the error.
- Author
- Recent Posts
Position Is Everything: Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL.
![]()
|
|
In a legacy C++ project that is using Boost ProgramOptions. trying to compile it will yield this error message:
In file included from /usr/include/boost/lexical_cast/detail/converter_lexical.hpp:50:0,
from /usr/include/boost/lexical_cast/try_lexical_convert.hpp:42,
from /usr/include/boost/lexical_cast.hpp:32,
from /usr/include/boost/program_options/value_semantic.hpp:14,
from /usr/include/boost/program_options/options_description.hpp:13,
from /usr/include/boost/program_options.hpp:15,
from /home/uli/dev/myproject/datasplit.cpp:15:
/usr/include/boost/array.hpp: In member function ‘T& boost::array<T, N>::operator[](boost::array<T, N>::size_type)’:
/usr/include/boost/array.hpp:118:61: error: expected primary-expression before ‘,’ token
return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
I didn’t find a satisfying way to fix this issue but it can be worked around fixing the issue in the source file:
First, open /usr/include/boost/array.hpp in your favourite editor as root (sudo!). I use nano.
Then, go to line 118 which reads:
return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
Replace that line by
BOOST_ASSERT_MSG( i < N, "out of range" ); return elems[i];
Also, 4 lines below what we just edited you’ll find another instance of
return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
Also replace that by
BOOST_ASSERT_MSG( i < N, "out of range" ); return elems[i];
Now. save the file and close your editor. Your code should compile now.
abstract declarator ‘TYPE’ used as declaration[edit | edit source]
- Message found in GCC version 4.5.1
- often grouped together with:
- member ‘DATA_MEMBER’ with constructor not allowed in anonymous aggregate
- member ‘DATA_MEMBER’ with destructor not allowed in anonymous aggregate
- member ‘DATA_MEMBER’ with copy assignment operator not allowed in anonymous aggregate
- often grouped together with:
- a class or struct is missing a name:
struct { // error, no name int bar; };
- a header file has a class or struct with a name already used inside ifndef, define statements
#ifndef foo #define foo #include <vector> struct foo { // error, foo already in use std::vector<int> bar; }; #endif
call of overloaded ‘FUNCTION’ is ambiguous[edit | edit source]
‘VARIABLE’ cannot appear in a constant-expression[edit | edit source]
‘VARIABLE’ cannot be used as a function[edit | edit source]
- Message found in GCC version 4.5.1
- make sure the variable name does not have an underscore in it (compiler weirdness)
- you’re using the same name for a variable name and a function inside a function definition
int foo(int baf) { return baf; } int bar(int foo) { foo = foo(4); return foo; }
conversion from ‘TYPE’ to non-scalar type ‘TYPE’ requested[edit | edit source]
- Message found in GCC version 4.5.1
- type conversion error, look for missing «::» syntax or missing parenthesis
- possibly a casting error
- a class member function returns a value that does not match the function’s declared return type
class Foo { public: int x; }; class Bar { public: Foo Maz() { return 0; } // 0 is of type int, not Foo };
could not convert ‘STATEMENT’ to ‘bool’[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- you a mistyped comparison operator (e.g., using: «=» instead of «==»)
- you used an incorrect return type for the called function’s definition
// you had: foo operator<(const foo & f) const // instead of: bool operator<(const foo & f) const
- you’re using an invalid argument for a conditional statement
string x = "foo"; if (x) cout << "true" << endl;
declaration of ‘FUNCTION’ outside of class is not definition[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- try using ‘=’ to initialize a value instead of parenthesis
- you used a semicolon or comma between a constructor and an initializer list instead of a colon
- you left a semicolon before the body of a function definition
class Foo { public: int bar; Foo(int x); }; Foo::Foo(int x); // semicolon ';' needs to be removed { bar = x; }
declaration of ‘VARIABLE’ shadows a parameter[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- you’re redefining a variable name that’s already in use, possibly declared in the function’s parameter list
int foo(int bar) { int bar; return bar; }
‘TYPE’ does not name a type[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC version 3.2.3 sometimes reported as: syntax error before ‘CHARACTER’ token
- in GCC version 4.0.1, sometimes reported as: ISO C++ forbids declaration
- e.g.: ISO C++ forbids declaration of ‘vector’ with no type
- you left out an object’s name qualifier or using directive
ostream & os; // instead of: std::ostream & os;
- make sure you didn’t mistype the scope operator «::», e.g.: «name:name» instead of «name::name»
- make sure you included the required libraries
#include <iostream> // missing vector library include class Foo { public: std::vector<int> Bar(std::vector<int> FooBar) { return FooBar; } };
- a header file is listed after a file that makes use of it in the include directives
// test.h file #ifndef TEST_H_ #define TEST_H_ std::string bar; #endif // test.cpp file #include "test.h" #include <iostream> // error, needed before test.h using namespace std; int main() { cout << bar << endl; return 0; }
expected ‘TOKEN’ before ‘TOKEN’ token[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- in GCC version 3.2.3 sometimes reported as: syntax error before ‘CHARACTER’ token
- check for a missing comma or parenthesis in a function’s parameters
- check for a missing semicolon
- e.g.: expected ‘,’ or ‘;’ before ‘TOKEN’
const int MAX = 10 // error int main() { string foo; cout << foo.size(); return 0; }
- possibly from a double namespace definition, or a fully-qualified (e.g., std::cout) name already under a ‘using’ directive
- possible missing ‘<<‘ or ‘>>’ operator in a cin/cout statement
int foo = 0, bar = 0; cin foo >> bar; // should be: cin >> foo >> bar;
expected primary-expression before ‘TOKEN’[edit | edit source]
- expected primary-expression before ‘int’
- Message found in GCC version 4.5.1
- in GCC version 3.2.3 reported as: parse error before ‘)’ token
- one likely cause is using (or leaving in) a type name in a function call
int sum(int x, int y) { return (x + y); } int main() { int a = 4, b = 5; sum(a, int b); // int is the problem causer return 0; }
expected unqualified-id before[edit | edit source]
- Message found in GCC version 4.5.1
- check your syntax for missing, misplaced, or erroneous characters
- expected unqualified-id before ‘(‘ token
- e.g.: parentheses in a class name
class Foo() { public: int x; };
- expected unqualified-id before ‘return’
- e.g.: missing opening brace in a conditional statement
int foo = 3, bar = 2; if (foo > bar) // error, no "{" cout << foo << endl; }
incompatible types in assignment of ‘TYPE’ to ‘TYPE’[edit | edit source]
- Message found in GCC versions 4.5.1
- you’re trying to assign to or initialize a character array using a character pointer
- e.g.: incompatible types in assignment of ‘const char*’ to ‘char [10]’
char bar[10]; const char *foo = "ppp"; bar = *foo; // error // possible fix, use strcpy from the cstring header: char bar[10]; const char *foo = "ppp"; strcpy(bar, foo);
- improperly accessing elements of a 2D array
char foo[2][3]; foo[1] = ' '; // error, need both dimensions, eg: foo[1][0] = ' ';
invalid conversion from ‘TYPE’ to ‘TYPE’[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- make sure parentheses were not left out of a function name
- make sure you are passing a function the correct arguments
char foo = 'f'; char bar[] = "bar"; if (strcmp(foo, bar) != 0) cout << "Correct answer!"; // strcmp was expecting 2 character pointers, foo doesn't qualify
invalid operands of types ‘TYPE’ and ‘TYPE’ to binary ‘FUNCTION’[edit | edit source]
- Message found in GCC version 4.5.1
- You’re trying to concatenate to C string arguments with the addition operator
// attempting to combine two C-strings cout << "abc" + "def"; // possible fix: convert 1 argument to a string type cout << "abc" + string("def");
invalid use of template-name[edit | edit source]
- invalid use of template-name ‘TEMPLATE’ without an argument list
- Message found in GCC version 4.5.1
- often paired with: expected unqualified-id before ‘TOKEN’
- in GCC version 3.2.3 reported as: syntax error before ‘CHARACTER’ token
- the type is missing after the class name in a function definition
template <class T> class Foo { private: int x; public: Foo(); }; template<class T> Foo::Foo() { x = 0; } // error, should be: Foo<T>::Foo()
is not a member of[edit | edit source]
- Message found in GCC versions 4.5.1
- check for a missing header include
- example: ‘cout’ is not a member of ‘std’
// test.cpp // file is missing iostream include directive int main() { std::cout << "hello, world!n"; return 0; }
‘TYPE’ is not a type[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC version 3.2.3 reported as: type specifier omitted for parameter ‘PARAMETER’
- you mistyped a template parameter in a function declaration
void foo(int x, vector y);
- an included header file does not have the correct libraries included in the source file to implement it:
- e.g.: you’re using #include «bar.h» without including the «foo.h» that «bar.h» needs to work
- Check that there are no methods with the same name as ‘TYPE’.
‘CLASS_MEMBER’ is private within this context[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- usually reported in the format:
- (LOCATION_OF_PRIVATE_DATA_MEMBER) error: ‘DATA_MEMBER’ is private
- (LOCATION_OF_CODE_ACCESSING_PRIVATE_DATA) error: within this context
- Message usually results from trying to access a private data member of a class or struct outside that class’s or struct’s definition
- Make sure a friend member function name is not misspelled
class FooBar { private: int bar; public: friend void foo(FooBar & f); }; void fooo(FooBar & f) { // error f.bar = 0; }
- make sure a read only function is using a ‘const’ argument type for the class
- make sure functions that alter data members are not const
- check for derived class constructors implicitly accessing private members of base classes
class Foo { private: Foo() {} public: Foo(int Num) {} }; class Bar : public Foo { public: Bar() {} // Bar() implicitly accesses Foo's private constructor };
- solution 1: use an initializer list to bypass implicit initialization
- solution 2: make the accessed base class member protected instead of private
- You’re trying to initialize a contained class member by accessing private data
class Foo { private: char mrStr[5]; public: Foo(const char *s = "blah") { strcpy(mrStr, s); } }; class Bar { private: int mrNum; Foo aFoo; public: Bar(int n, const Foo &f); }; // error, attempting to use the Foo class constructor by accessing private data: Bar::Bar(int n, const Foo &f) : aFoo(f.mrStr) { // mrStr is private mrNum = n; }
possible fix, assign the whole object rather than part of it:
Bar::Bar(int n, const Foo &f) : aFoo(f) { mrNum = n; }
ISO C++ forbids declaration of ‘FUNCTION’ with no type[edit | edit source]
- Message found in GCC version 3.2.3, 4.5.1
- you’ve created a function with no listed return type
Foo() { return 0: } // should be: int Foo() { return 0: }
multiple definitions of[edit | edit source]
- eg: multiple definition of `main’
- Message found in GCC version 4.5.1
- check for missing inclusion guard in header file
- check for duplicate file listing in compile commands / makefile
- e.g.: g++ -o foo foo.cpp foo.cpp
- check for definitions rather than only declarations in the header file
‘CLASS FUNCTION(ARGUMENTS)’ must have an argument of class or enumerated type[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- you’re attempting to access members of a class with a non-member function
- non-member functions must access class members explicitly
- eg: CLASS_NAME FUNCTION_NAME(CLASS_NAME OBJECT_NAME, ARGUMENTS)
- you’re redefining an operator for a standard (built-in) type
class Foo { public: friend int operator+(int x, int y); };
new types may not be defined in a return type[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC version 3.2.3, reported as:
-
- semicolon missing after definition of ‘CLASS’
- ISO C++ forbids defining types within return type
- check for a missing semicolon at the end of a class definition
class Foo { public: int x; } // Error
no match for call to ‘FUNCTION’[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- make sure the function’s namespace is used ( using namespace std / std::function() )
- make sure the function name is not misspelled, parentheses aren’t missing
- make sure the function is called with the correct arguments / types / class
- if you’re initializing a variable via parentheses, if there’s underscores in the variable name try removing them. Sometimes an equals sign is the only way…
- you’re using the same name for a variable and a function within the same namespace
string bar() { string foo = "blah"; return foo; } int main() { string bar; bar = bar(); // error, "bar()" was hidden by string initialization return 0; }
no matching function for call to ‘FUNCTION’[edit | edit source]
- Message found in GCC version 4.5.1
- make sure there aren’t parentheses where there shouldn’t be (e.g.: classname::value() instead of classname::value )
- you’re using a string argument with a function that expects a C-string
// broken code ifstream in; string MrString = "file.txt"; in.open(MrString); // solution: convert the string to a C-string ifstream in; string MrString = "file.txt"; in.open(MrString.c_str());
non-constant ‘VARIABLE’ cannot be used as template argument[edit | edit source]
- Message found in GCC version 3.2.3
- in GCC version 4.5.1 reported as: ‘VARIABLE’ cannot appear in a constant-expression
- variable used for a template argument, which are required to be constant at compile time
template <class T, int num> class Bar { private: T Foo[num]; }; int main() { int woz = 8; Bar<double, woz> Zed; // error, woz is not a constant return 0; }
non-member function ‘FUNCTION’ cannot have cv-qualifier[edit | edit source]
- error: non-member function ‘int Foo()’ cannot have cv-qualifier
- cv = constant / volatile
- Message found in GCC version 4.5.1
- you’re using the ‘post’ const (constant value) on a non-member function
- you’re not using the scope qualifier («TYPENAME::») in the function definition
- you mistyped the definition for a template class’s member function
template<class Type> class Foo { private: int stuff; public: int bar() const; }; template<class Type> int Foo::bar() const { // error return stuff; }
possible fix:
template<class Type> int Foo<Type>::bar() const { return stuff; }
passing ‘const OBJECT’ as ‘this’ argument of ‘FUNCTION’ discards qualifiers[edit | edit source]
- Message found in GCC version 4.5.1
- you’re returning an address
- you’re attempting to access a container element with a const_iterator using a member function that has no non-const versions. The non-const function does not guarantee it will not alter the data
request for member ‘NAME’ in ‘NAME’, which is of non-class type ‘CLASS’[edit | edit source]
- Message found in GCC versions 4.5.1
- in GCC version 3.2.3 reported as:
-
- request for member ‘NAME’ in ‘NAME’, which is of non-aggregate type ‘TYPE’
- check the function call in the code, it might be calling a function with incorrect arguments or it might have misplaced/missing parenthesis
- your using the «*this» pointer where you should just be using the functions name
- e.g., use: return mem_func(); instead of: return *this.mem_func();
- using the «*this» pointer with the wrong syntax
class Foo { public: int x; Foo(int num = 0) { x = num; } void newX(int num); }; void Foo::newX(int num) { *this.newX(num); // error, need (*this).newX or this->newX }
statement cannot resolve address of overloaded function[edit | edit source]
- Message found in GCC versions 3.2.3, 4.5.1
- make sure you’re not forgetting the parenthesis after a member function name
class Foo { public: int Bar() { return 0; } }; int main() { Foo x; x.Bar; // error return 0; }
two or more data types in declaration of ‘NAME’[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC version 3.2.3 reported as: extraneous ‘TYPE’ ignored
- you have multiple data types listed for a function declaration’s return value
int char sum(int x, int y); // int char
- possibly a missing semicolon in between 2 type declarations
- usually missing in a function, struct, or class declaration after the curly braces {}
<GOBBLEDEGOOK> undefined reference to <GOBBLEDEGOOK>[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC versions 4.0.1, 4.2.1 reported as: Undefined symbols
- check for a missing or mistyped header includes
- check for a missing or mistyped files/libraries in a project/make file
- check for a missing, mistyped, or undefined functions or class constructors
// header file void foo(); void bar(); void baz(); // implementation file, bar definition is missing void foo() { cout << "foon"; } void baz() { cout << "bazn"; }
- check for function declarations that do not match their definitions
- make sure function names do not overlap those in existing header files
- make sure compile commands syntax / makefile structure is correct (e.g.: g++ -o file.cc … etc.)
- no main() function is defined in any of the files inside a project/makefile
- e.g.: undefined reference to `WinMain@16′
‘NAME’ was not declared in this scope[edit | edit source]
- Message found in GCC version 4.5.1
- in GCC versions 3.2.3 reported as: ‘FUNCTION’ undeclared (first use of this function)
- look for a misspelled or changed variable/function/header call name
lonh wait; // instead of: long wait;
- make sure the proper header and library files are included
- defined variables may need the headers they utilize included in all files that use the defined variables