What could be the possible error in this line :
double bx;
bx I define universally ! but I am getting the above error on compiling .Even for float bx; I get the same kind of error .
The surrounding code is :
#include "header.h"
#include "ball_pad.h"
#include "pingpong.h"
#include "texture.h"
#include "3dsloader.h"
float A = 90.0f;
float B = 70.0f;
/**********************************************************
*
* VARIABLES DECLARATION
*
*********************************************************/
// The width and height of your window, change them as you like
int screen_width=640;
int screen_height=480;
// Absolute rotation values (0-359 degrees) and rotation increments for each frame
double rotation_x=0, rotation_x_increment=0.1;
double rotation_y=0, rotation_y_increment=0.05;
double rotation_z=0, rotation_z_increment=0.03;
// Absolute rotation values (0-359 degrees) and rotation increments for each frame
double translation_x=0, translation_x_increment=1.0;
double translation_y=0, translation_y_increment=0.05;
double translation_z=0, translation_z_increment=0.03;
// Flag for rendering as lines or filled polygons
int filling=1; //0=OFF 1=ON
//Now the object is generic, the cube has annoyed us a little bit, or not?
obj_type board,ball,pad_alongX,pad_alongY;
BALL ball1//,ball2,ball3;
double bx = 0;
double by = 0;
double bvx = 2.0;
double bvy = 2.0;
double radius = 5.0;
//ball2.bx = 0;ball2.by = 0;ball2.bvx = 2.0;ball2.bvy = 2.0;ball2.radius = 5.0;
//ball3.bx = 0;ball3.by = 0;ball3.bvx = 2.0;ball3.bvy = 2.0;ball3.radius = 5.0;
PADDLE pad1,pad2,pad3,pad4;
//pad1.px = 0;pad1.py = 0;pad1.pvx = 2.0;pad.pvy = 2.0;pad1.length = 25.0;pad1.width = 5.0;
Содержание
- Error expected initializer before double
- Error expected initializer before double
- Error expected initializer before double
- Error expected initializer before double
- Error expected initializer before double
Error expected initializer before double
I Keep getting this error message, and i’m sure it has something to do with how I am attempting to call my functions. I have been at this for hours, what am I doing wrong?
error: expected initializer before ‘double’
double tsav(int tstotal,int tsgradecounter, int grade)
using namespace std;
double qzav(int qztotal,int qzgradecounter, int grade)
double tsav(int tstotal,int tsgradecounter, int grade)
double hwav(int hwtotal,int hwgradecounter, int grade)
int option; //option for menu
string firstname;
double qzaverage= qzav(qztotal,qzgradecounter,grade);
double tsaverage= tsav(tstotal,tsgradecounter,grade);
double hwaverage= hwav(hwtotal,hwgradecounter,grade);
cout > option; //print option
cout>> «Quiz»>> endl;
cin >»Test»>> endl;
cin >»Homework»>> endl;
cin > «Quiz averager»;
cout > grade;
qztotal= qztotal + grade; //total quiz grade
qzgradecounter= qzgradecounter + 1;
if (qzgradecounter !=0)
qzaverage= static_cast (qztotal)/ qzgradecounter;
tstotal= tstotal + grade;
tsgradecounter= tsgradecounter + 1;
if (tsgradecounter !=0)
tsaverage= static_cast (tstotal)/ tsgradecounter;
hwtotal= hwtotal + grade;
hwgradecounter= hwgradecounter + 1;
if (hwgradecounter !=0)
hwaverage= static_cast (hwtotal)/ hwgradecounter;
Источник
Error expected initializer before double
I have not a lot of experience with C++. So far I did everything in one file. Now I tried to use 2 source files and 1 header.
I get the message that an initializer is expected.
This is the code of the mein source file (where the error is reported)
#include
#include
#include
#include «subs.h»
using namespace std;
int main()
<
int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks
double steplength(1); //length of one step
double *R;
R= new double[N2];
//initial seed
int seed (11324);
#include
#include
#include
using namespace std;
double randomwalk(int seed, int N, double steplength) <
//random number generator
unsigned int c(16807);
unsigned int p(21474837);
unsigned int *r;
double *randomnumb;
r= new unsigned int[N+1];
randomnumb=new double [N];
r[0]=seed;
randomnumb[0]=r[0];
randomnumb[0]=randomnumb[0]/p;
//generate random numbers
for(int i=1;i
Sorry, the last line «and the header» should be above:
#ifndef SUBS_H_INCLUDED
#define SUBS_H_INCLUDED
int N(10000); //number of steps for the randomwalk
int N2(1000); //number of randomwalks
These values, Right?
What is with these values?
The error message is: line 6 error: expected initializer before using
There is some problem with an initializer, but I don’t know what this means.
correct me if I’m wrong but you have 3 files
If this is what you mean your on the right track, but you need to understand how compiling works.
To put it in a simple way, the compiler will compile every cpp file separately first, doing a #include «something.h» will tell the compiler that some thing you call will be found in a other file (in this case something.h. But at this point the compiler has no clue where the code is only what will be in it. After this the linker will combine all these files to one binary. In your case the linker can’t find where the actual code for subs.h is (sins header and source files don’t need to have the same name or even be in the same location) This is why a source file needs to include it’s own header file to let the linker know what header file is linked to what source file. So you only need to add #include «subs.h» to the top of your subs.cpp file:
And since you are using header safeguards the actual source will only be inserted ones (upon the first include) in the final binary.
(In a actual compiler and more advanced stuff it’s a bit more complicated then this, and the compiler/linker does many more things. But that’s stuff you don’t touch unless you know what you are doing.)
Ok, I added #include «subs.h» into my randomwalk.cpp file. I still get an error «expected initializer before double». It is in the randomwalk.cpp file. (6. Line)
To your first question, yes I have 3 files, only that the file subs.cpp is called randomwalk.cpp .
This is what I compiled on my computer and it worked fine:
If you still get a error with this, it’s not in the code.
What is the command you use to compile and link?
Источник
Error expected initializer before double
I’m writing a class with an array and I’m supposed to calculate the average of some entered grades outside the class and in the main but I get the error
Expected Initializer before ‘.’ token
I’m not to sure what to do or i even did the class right
Please help!
What if I promised you $1000, but then just walk away, leaving you empty handed?
You make a promise on line 24, but you don’t keep it.
The error is on line 44. Please explain this code:
float gradeAverage.getGrade();
Line 44 looks like a nice prototype, but what is it doing in the middle of the program?
In the function «getGrade()» Why are you printing the array when you should be returning something from the array. In this case the «getGrade()» function should have a parameter the represents the element of the array that you need.
As you have it «float getGrade ()» would be better named «void printGrades ()».
Hope that helps,
I know that i shouldn’t be printing the there but the question was my teacher gave me was this
Write a class called Student that contains a property called grades that can store a
maximum of 10 grades. Create a setter and a getter method. The setter method
will take no parameters and return no parameters. Instead, within the setter
method you must construct a loop that will ask the user to enter 10 grades. The
getter method will simply print the 10 grades; so, it will take no and return no
parameters. Yes, that is a misnomer; this is because passing and returning arrays
has not been covered yet. Create another method called computeAverage that
return the average of all the grades. Create another method called
minimumGrade that returns the minimum grade the student received.
He knows it’s a misnomer but he wants us to do it anyway.
He hasn’t covered it yet so I was just looking it up but nothing I find helps
Now that I see what is required I will take a look tomorrow and see what I can do.
Источник
Error expected initializer before double
I am trying to make a calculator program and I am getting an error when I try to define a function from a class template
I get an error here:
template void Calculator ::set_operand_one()
You have variables mOperand1 etc but the constructor has operand1 etc — the spelling difference is part of what the compiler is complaining about.
You’ll need this include for vector.
#include
Edit:
See this earlier answer about initializing a static variable.
http://www.cplusplus.com/forum/beginner/100701/
I don’t think anyone is understanding.
The problem is not in the constructor, it is here:
void Calculator ::set_operand_one(T op1)
it gives an error: expected initializer before ‘
I copied and pasted your code into the compiler on this site and got those errors.
The class declaration is usually in the .h file. The class function definitions are usually in the .cpp file. You seem to have reversed these above.
Thank you everyone for your replies, but I finally figured out the problem.
I accidentally put the declaration of the class in the .cpp file and the definition in the .h file, when it should be the reverse of that! I apologize for making this stupid mistake. I will mark this thread as solved for others that make the same mistake 🙂
Источник
Error expected initializer before double
prog3.cpp:10:1: error: expected initializer before ‘int’
int main ()
^
this is what i get when i run this program please any help regarding this it will be much appreciated and please tell me if i made this program workable like with boolean expression if the function and prototype are well declared and performing together
/*This program ask user to input Hour, Minute and seconds and if the user
put in the valid format it runs othervise it says error. */
bool readTime(int &hours, int &minutes, int &seconds)
int main ()
<
int h,m,s;
if(readTime(h,m,s)) <
printf(«%2d:%2d:%2d» h, m, s);
return 0;
>
>
printf(«please enter time in format 09:30:50n»);
int count = scanf(«%2d:%2d:%2d», &hh, &mm, &ss);
>
// did they get the right format?
if (count !=3) <
printf(«Invalid formatn»);
return false;
>
// is the number of hours correct?
if ((hh 23)) <
printf(«Invalid hour %d n», hh);
return false;
>
//is number of minutes wrong?
if ((mm 0)) <
printf(«Invalid Minutes %d n», mm);
return false;
>
//is number of seconds wrong?
if ((ss 0)) <
printf(«Invalid seconds %d n», mm);
return false;
>
now this is what i get after doing your instructions
prog3.cpp: In function ‘int main()’:
prog3.cpp:14:28: error: expected ‘)’ before ‘h’
printf(«%2d:%2d:%2d» h, m, s);
^
prog3.cpp:14:35: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
printf(«%2d:%2d:%2d» h, m, s);
^
prog3.cpp: In function ‘bool readTime(int&, int&, int&)’:
prog3.cpp:24:8: warning: unused variable ‘count’ [-Wunused-variable]
int count = scanf(«%2d:%2d:%2d», &hh, &mm, &ss);
^
prog3.cpp:25:4: warning: no return statement in function returning non-void [-Wreturn-type]
>
^
prog3.cpp: At global scope:
prog3.cpp:27:4: error: expected unqualified-id before ‘if’
if (count !=3) <
^
prog3.cpp:32:4: error: expected unqualified-id before ‘if’
if ((hh 23)) <
^
prog3.cpp:37:4: error: expected unqualified-id before ‘if’
if ((mm 0)) <
^
prog3.cpp:42:4: error: expected unqualified-id before ‘if’
if ((ss 0)) <
^
prog3.cpp:47:1: error: expected declaration before ‘>’ token
>
^
You have extra braces.
You will find it extremely helpful to
1. Indent your code consistently. Use an editor which does this for you.
2. Type the closing brace at the same type you type the opening brace. Use an editor which does this for you.
Here is a «fixed» version. (there’s still some issues, but it will compile with warnings.)
i run your program but then i see this issue it will be helpful if i get perfectly running program so that i know for my upcoming exam how to make this type of program run. I am currently preparing for my exam. hope to see some help from you guys out there
In function ‘int main()’:
9:26: error: expected ‘)’ before ‘h’
9:33: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
In function ‘bool readTime(int&, int&, int&)’:
41:1: warning: control reaches end of non-void function [-Wreturn-type]
This line :
printf( «%2d:%2d:%2d» h, m, s);
Should be :
printf( «%2d:%2d:%2d» , h, m, s); // An extra comma (,)
thank you for the help but why am i facing problem like this?
progs3.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
>
^
Notice that the compiler provides you with the location of any errors that it encounters.
Line 9, column 26 — usually there’s a filename, too. There’s a missing comma (I missed it, apologies.)
If the problem is a syntax error, the compiler will report the error no earlier than it appears. Look at the location of the first error and then look backwards towards the beginning of the file until you find it.
thank you for the help its so much appreciated and helpful for my upcoming exam and future programming errors. but can you help me with my issue on line 41?
Источник
Please help:: Error message expected initializer before ‘double’
I Keep getting this error message, and i’m sure it has something to do with how I am attempting to call my functions. I have been at this for hours, what am I doing wrong?
error: expected initializer before ‘double’
double tsav(int tstotal,int tsgradecounter, int grade)
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
double qzav(int qztotal,int qzgradecounter, int grade)
double tsav(int tstotal,int tsgradecounter, int grade)
double hwav(int hwtotal,int hwgradecounter, int grade)
int main()
{
int option; //option for menu
string lastname;
string firstname;
double qzaverage= qzav(qztotal,qzgradecounter,grade);
double tsaverage= tsav(tstotal,tsgradecounter,grade);
double hwaverage= hwav(hwtotal,hwgradecounter,grade);
do
{
cout << «1) Average grades for a new student:» <<endl; //menu for average
cout << «2) Quit program» <<endl; //option to quit
cout << «Please select an option»; //prompt user to select menu option
cin >> option; //print option
if(option == 1)
{
cout<<«Enter students first name»;
cin>> firstname;
cout<<«Enter students last name»<<endl;
cin>> lastname;
cout>> «Quiz»>> endl;
cin<< qzaverage;
cout>>»Test»>> endl;
cin<< tsaverage;
cout>>»Homework»>> endl;
cin<< hwaverage;
}
else if(option ==2)
{
cout<< «terminating program»;
}
}
while(option != 2);
}
double qzav(int qztotal,int qzgradecounter,int grade)
{
int qztotal=0;
int qzgradecounter=0;
int grade;
cout>> «Quiz averager»;
cout<< «Enter Quiz grade or -1 to quit:»; //propmt to enter qz grade
cin>> grade;
while (grade != -1)
{
qztotal= qztotal + grade; //total quiz grade
qzgradecounter= qzgradecounter + 1;
cout<< «Enter Quiz grade or -1 to quit:»;
cin>> grade;
}
if (qzgradecounter !=0)
{
qzaverage= static_cast<double>(qztotal)/ qzgradecounter;
cout<<«total of all grades» <<qztotal<< endl;
cout<< «Quiz average is» << setprecision(2)<<fixed<<qzaverage<< endl;
}
else
cout << «No Grades were entered»<< endl;
return qzaverage;
}
}
double tsav(int tstotal,int tsgradecounter, int grade)
{
int tstotal=0;
int tsgradecounter=0;
int grade; //actual grade
cout<< «Enter Test grade or -1 to quit:»;
cin>> grade;
while (grade != -1)
{
tstotal= tstotal + grade;
tsgradecounter= tsgradecounter + 1;
cout<< «Enter Test grade or -1 to quit:»;
cin>> grade;
}
if (tsgradecounter !=0)
{
tsaverage= static_cast<double>(tstotal)/ tsgradecounter;
cout<<«total of all grades» <<tstotal<< endl;
cout<< «Test average is» << setprecision(2)<<fixed<<tsaverage<< endl;
}
else
cout << «No Grades were entered»<< endl;
return tsaverage
}
}
double hwav(int hwtotal,int hwgradecounter, int grade)
{
int hwtotal=0; //quiz total
int hwgradecounter=0; //number of grades
int grade; //actual grade
cout<< «Enter Homework grade or -1 to quit:»;
cin>> grade;
while (grade != -1)
{
hwtotal= hwtotal + grade;
hwgradecounter= hwgradecounter + 1;
cout<< «Enter Homework grade or -1 to quit:»; //propmpt to enter hw grade
cin>> grade;
}
if (hwgradecounter !=0)
{
hwaverage= static_cast<double>(hwtotal)/ hwgradecounter;
cout<<«total of all grades» <<hwtotal<< endl;
cout<< «Homework average is» << setprecision(2)<<fixed<<hwaverage<< endl;
return hwaverage;
}
}
please use code tags. look at line one. thats where the error is.
Topic archived. No new replies allowed.
Loading
BalexD 0 / 0 / 0 Регистрация: 26.05.2013 Сообщений: 16 |
||||
1 |
||||
28.12.2013, 21:41. Показов 30160. Ответов 11 Метки нет (Все метки)
В общем, компилятор почему-то ругается на 3 строку, говоря «expected initializer before void» Что ему тут не нравится — ума не приложу. Все функции есть в хидере, ругаться стал недавно — ранее работал на ура. Перестраивала, перезапускала блокс(на всяк пожарный), а толку нет. Вот сам код:
__________________
0 |
Модератор 12641 / 10135 / 6102 Регистрация: 18.12.2011 Сообщений: 27,170 |
|
28.12.2013, 21:48 |
2 |
Что-то не так в HW_C.h
0 |
1672 / 1044 / 174 Регистрация: 27.09.2009 Сообщений: 1,945 |
|
28.12.2013, 21:48 |
3 |
Логично предположить, что ошибка в файле HW_C.h. Например, отсутствие точки с запятой после определения структуры.
0 |
17 / 17 / 2 Регистрация: 03.05.2013 Сообщений: 114 |
|
28.12.2013, 21:50 |
4 |
Что-то в заголовочном файле не так. Покажите нам его, пожалуйста.
0 |
BalexD 0 / 0 / 0 Регистрация: 26.05.2013 Сообщений: 16 |
||||
28.12.2013, 22:07 [ТС] |
5 |
|||
Вот та самая часть хидера:
0 |
Модератор 12641 / 10135 / 6102 Регистрация: 18.12.2011 Сообщений: 27,170 |
|
29.12.2013, 11:03 |
6 |
У меня это все компилируется.
0 |
0 / 0 / 0 Регистрация: 26.05.2013 Сообщений: 16 |
|
03.01.2014, 02:07 [ТС] |
7 |
Проблема в том, что HW_C.h — один-единственный. И других просто нету. Уж не знаю, на что грешить(
0 |
5493 / 4888 / 831 Регистрация: 04.06.2011 Сообщений: 13,587 |
|
03.01.2014, 02:14 |
8 |
Весь проект выкладывайте.
0 |
0 / 0 / 0 Регистрация: 26.05.2013 Сообщений: 16 |
|
08.01.2014, 20:02 [ТС] |
9 |
Ну, кидаю в виде ссылки. Чтоб никто не терялся: сама функция используется в HW_Dop1.cpp, а в хидере прописана в самом конце.
0 |
5493 / 4888 / 831 Регистрация: 04.06.2011 Сообщений: 13,587 |
|
09.01.2014, 04:58 |
10 |
Вот результат компиляции этого проекта в Code::Blocks 12.11: objDebugHW_Dop1.o||In function `Z13insert_4_numbv’:| И при чём здесь тогда:
компилятор почему-то ругается на 3 строку, говоря «expected initializer before void» ??? Добавлено через 1 минуту
сама функция используется в HW_Dop1.cpp, а в хидере прописана в самом конце.
1 |
0 / 0 / 0 Регистрация: 26.05.2013 Сообщений: 16 |
|
10.01.2014, 00:02 [ТС] |
11 |
Хм.. Странно. У меня все выглядит немного по-другому. А про заголовочный файл скажу, что никто мне не говорил, что так делать нельзя. По крайней мере, до этого момента ошибки не возникало. Что же, учтем и эти грабли. Но спасибо за указания на промахи. Искренне всем благодарна)
0 |
5493 / 4888 / 831 Регистрация: 04.06.2011 Сообщений: 13,587 |
|
10.01.2014, 00:07 |
12 |
Хм.. Странно. У меня все выглядит немного по-другому. Интересно было бы посмотреть.
0 |