Code blocks C++ class constructor error - c++

I'm trying to create the contructors for line but I keep getting this error even though this worked on the poolball class and was able to run before adding line.cpp and line.h. Also, this is code from class, and i have no idea why it is not compiling.
#include "Line.h"
Line::Line(){
}
Line::Line( int x1, int y1, int x2, int y2){
}
void Line::setPos(int x1, int y1, int x2, int y2){
}
void Line::draw( void ){
}
-------------------------------------------------------------------------
#pragma once
class Line{
public:
int x1;
int y1;
int x2, y2;
//constructor
Line();
Line( int x1, int y1, int x2, int y2);
//methods
void setPos(int x1, int y1, int x2, int y2);
void draw( void);
This is the error I get when trying to build and run
||=== Build: Debug in 10.19class (compiler: GNU GCC Compiler) ===|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|2|error: extra qualification 'Line::' on member 'Line' [-fpermissive]|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|2|error: 'Line::Line()' cannot be overloaded|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.h|10|error: with 'Line::Line()'|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|5|error: extra qualification 'Line::' on member 'Line' [-fpermissive]|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|5|error: 'Line::Line(int, int, int, int)' cannot be overloaded|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.h|11|error: with 'Line::Line(int, int, int, int)'|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|9|error: extra qualification 'Line::' on member 'setPos' [-fpermissive]|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|9|error: 'void Line::setPos(int, int, int, int)' cannot be overloaded|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.h|13|error: with 'void Line::setPos(int, int, int, int)'|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|12|error: extra qualification 'Line::' on member 'draw' [-fpermissive]|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|12|error: 'void Line::draw()' cannot be overloaded|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.h|14|error: with 'void Line::draw()'|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|13|error: expected '}' at end of input|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp||In constructor 'Line::Line(int, int, int, int)':|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|6|error: 'cout' is not a member of 'std'|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|6|error: 'endl' was not declared in this scope|
C:\Users\Admin\Desktop\C++Projects\10.19class\Line.cpp|13|error: expected unqualified-id at end of input|
||=== Build failed: 16 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Judging by the error messages, and assuming the stuff below the dashes is the full contents of your header file, then your problem is that you're missing }; at the end of your header file. You need to close your class definition with the close bracket and semicolon in your header, before you can implement the functions in your Line.cpp file.

void Line(void) is not true,constructors have no return type and in parameter list you shouldn't write void,either.

Related

How to fix "too few arguments to function"?

my program is given below
#include <iostream>
using namespace std;
int sum(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int pro(int x,int y)
{
return x*y;
}
int quo(int x,int y)
{
return x/y;
}
int main()
{
int a,b;
char op;
cout<<"Enter two numbers:"<<endl;
cin>>a>>b;
cout<<"Enter a operator:"<<endl;
cin>>op;
switch(op)
{
case '+':
sum(a,b);
cout<<sum()<<endl;
break;
case '-':
sub(a,b);
cout<< sub()<<endl;
break;
case '*':
pro(a,b);
cout<< pro()<<endl;
break;
case '/':
quo(a,b);
cout<< quo() <<endl;
break;
default:
cout<<"Invalid Operator"<<endl;
}
return 0;
}
here is the error i receive
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\amohe\Desktop\cal.cc||In function 'int main()':|
C:\Users\amohe\Desktop\cal.cc|33|error: too few arguments to function 'int sum(int, int)'|
C:\Users\amohe\Desktop\cal.cc|4|note: declared here|
C:\Users\amohe\Desktop\cal.cc|37|error: too few arguments to function 'int sub(int, int)'|
C:\Users\amohe\Desktop\cal.cc|8|note: declared here|
C:\Users\amohe\Desktop\cal.cc|41|error: too few arguments to function 'int pro(int, int)'|
C:\Users\amohe\Desktop\cal.cc|12|note: declared here|
C:\Users\amohe\Desktop\cal.cc|45|error: too few arguments to function 'int quo(int, int)'|
C:\Users\amohe\Desktop\cal.cc|16|note: declared here|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
need solution.
If you read the quite clear error message, including the line number, then you will notice that it does NOT complain on
sum(a,b);
But it DOES complain on
cout<<sum()<<endl;
The difference is rather striking.
You need to give enough parameters to the second one.
Also the first one is useless, compilers tend to tell you that if you increase the warning level.
A solution is to not only calculate and ignore it, instead output it directly and corrctly by using a gentle mixture of your two code lines.
cout<<sum(a,b)<<endl;

C++ Compiling Issues

When compiled using g++ MainStudent.cpp Student.cpp
These are the errors i get :
MainStudent.cpp: In function ‘int main()’: MainStudent.cpp:23:38:
error: no matching function for call to ‘Student::Student(char [10],
char [10], int&, double [3])’ MainStudent.cpp:23:38: note: candidates
are: Student.h:13:2: note: Student::Student(char*, char*, int, double)
Student.h:13:2: note: no known conversion for argument 4 from
‘double [3]’ to ‘double’ Student.h:5:7: note: Student::Student(const
Student&) Student.h:5:7: note: candidate expects 1 argument, 4
provided Student.cpp: In constructor ‘Student::Student(char*, char*,
int, double)’: Student.cpp:9:11: error: incompatible types in
assignment of ‘double’ to ‘double [3]’ Student.cpp: At global scope:
Student.cpp:14:5: error: prototype for ‘int Student::Getage()’ does
not match any in class ‘Student’ Student.h:16:7: error: candidate is:
int* Student::Getage() Student.cpp:15:8: error: prototype for ‘double
Student::Getmarks()’ does not match any in class ‘Student’
Student.h:17:10: error: candidate is: double* Student::Getmarks()
I can't figure out where the problem lies...
Your constructor is
Student::Student (char *fname, char *lname, int age, double marks)
^^^^^^^^^^^^
But you are trying to pass an array to it in
double marks[3];
//...
Student st1(fname, lname, age, marks);
You either need to get rid of the array in the class and just take a double or change the constructor to take a double array and then copy it in the constructor like
Student::Student (char *fname, char *lname, int age, const double (&marks)[3]) {
// ^^^^^^^^^^^^^^^ use array of size 3
// since that is what _marks is
strcpy(_fname, fname);
strcpy(_lname, lname);
_age = age;
for (int i = 0; i < 3; i++)
_marks[i] = marks[i];
}

Header File Not Seeing Other Header Identifier

I have been having issues with Visual Studio 2013. I can't explain why its happening. The compile issue is shown in the pictures below.
I have one class "PhysicsEngine.cpp/h" that use the class "RigidBody.cpp/h" just fine. Once I attempt to use the RigidBody class in my Character class header, it fails to see the RigidBody class identifier. I have included "RigidBody.h" in my file, and even tried prototyping the class. Interesting enough when I delete the "RigidBody.h" header inclusion, it spits out even more errors, leading me to believe it is reading it. Is there something I'm doing wrong?
Here is the output I received as an error:
1>------ Rebuild All started: Project: SideScroller, Configuration: Debug Win32 ------
1> WorldChunkManager.cpp
1> World.cpp
1> Vector.cpp
1>e:(directory)\vector.cpp(41): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1>e:(directory)\vector.cpp(48): warning C4244: 'argument' : conversion from 'double' to 'float', possible loss of data
1> Tile.cpp
1> RigidBody.cpp
1>e:(directory)\character.h(21): error C2146: syntax error : missing ';' before identifier 'pos'
1>e:(directory)\character.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:(directory)\character.h(21): error C2143: syntax error : missing ';' before '='
1>e:(directory)\character.h(21): error C2238: unexpected token(s) preceding ';'
Here is the code for the RigidBody.h
#ifndef RH
#define RH
#include "Vector.h"
#include "World.h"
#include <cmath>
#include <list>
class RigidBody
{
private:
int id;
float gravityFactor;
float airResistFactor;
float mass;
float elasticity;
Vector pos = Vector(0, 0);
int width;
int height;
Vector velocity = Vector(0, 0);
std::list<Vector>* additiveVelocities;
std::list<Vector>* forces;
public:
RigidBody(int x, int y, int w, int h, float mass, float elasticity, float gravityFactor, float airResistFactor);
~RigidBody();
static int rigidBodyCount;
//Get/Set
int getID();
double getX();
double getY();
float getGravFact();
float getAirFact();
float getMass();
float getElast();
Vector getPos();
Vector getVelocity();
std::list<Vector>* getadditiveVelocities();
std::list<Vector>* getForces();
void setX(double x);
void setY(double y);
void setVelocity(Vector& vec);
void setPos(Vector& vec);
//Interactino Functions
void addForce(Vector force);
void addVelocity(Vector velocity);
};
#endif
Here is the code that will not work (Character.h):
#ifndef CHARACTER_H
#define CHARACTER_H
#include "SDL.h"
#include "Camera.h"
#include "InputManager.h"
#include "Vector.h"
#include "RigidBody.h"
//TEMP DIM
const int CHAR_H = 64;
const int CHAR_W = 12;
class Character
{
private:
double speed;
//Vector pos = Vector(0, 0);
RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);
InputManager* inputPtr;
public:
Character(int x, int y, InputManager* inputPtr);
void getXY(int* dest);
void getChXY(int* dest);
void getCenterXY(int* dest);
//Update
void update(long double last);
bool isFloor();
//Draw
void draw(SDL_Surface* screen, Camera* camPtr);
};
#endif
Let me know if there is anything additional you could need to find the problem!!! I don't want to overload the page with potentially irreverent information. Thank you!
I think the problem with this line isn't that it's failing to find an identifier:
RigidBody pos = RigidBody(0, 0, CHAR_W, CHAR_H, 50, 0, 1, 1);
Rather, it's that you're trying to do an illegal initialization, in a C#-type way. You can't just assign a value to a class member as part of its declaration like that in C++. You need to assign the initial value in the constructor, or in an initializer attached to the constructor.

C++ typedef static function pointer: undefined symbol

//class.h
typedef double (*ffunct)(double x1, double y1, double x2, double y2);
class Class {
public:
static ffunct myfunct;
static void setFunct();
static double doSomething(double x1, double y1, double x2, double y2);
static void call();
}
//class.cpp
void Class::setFunct(){
Class::myfunct=Class::doSomething;
}
double Class::doSomething(double x1, double y1, double x2, double y2) {
cout << "Hello World" << endl;
}
void Class::call() {
Class::myfunct(1.0,2.0,3.0,4.0);
}
//main.cpp
…
Class::setFunct();
Class::call();
…
Running the programm results in Undefined symbols for architecture x86_64: "Class::myfunct", referenced from Class::setFunct, Class::call…
So what am I doing wrong?
In your cpp file you need one more line:
ffunct Class::myfunct = NULL;
The class declaration said that the variable would exist somewhere but you never gave it a definition. Since it's not part of each object it has to be defined separately.
Your prototype of doSomething has a return type of double, but your implementation thereof has a return type of void.
static double doSomething(...)
...
void Class::doSomething(...)
Fixing this won't clear all the errors however. You still have a few more as the other answers mention.
static ffunct myfunct; is a declaration
You need a definition of it also in the cpp file
ffunct Class::myfunct;
add one line on the top of your .cpp file:
ffunct Class::myfunct=NULL;

C++ redifinition of'..' and previously declared here errors

I have already searched for this type of error and found a few threads, but each one recommended using #ifndef to make sure the header file is only loaded once. I have done this and still get an error. The odd thing is this error:
circle.cc:25:6: error: prototype for ‘void circle::populate_classobj(int, int, int)’ does not match any in class ‘circle’
says my function only has 3 int's but every place i have that function, i have 4 ints.
here is my class header file
#ifndef _CIRCLE_H_
#define _CIRCLE_H_
#define PI 3.14159
class circle
{
public:
float radius(int x1, int x2, int y1, int y2);
float circumference(float d);
float area(float d);
void populate_classobj(int, int, int, int);
protected:
float distance(int x1, int x2, int y1, int y2);
private:
int x1, y1, x2, y2;
};
#endif // _CIRCLE_H_
Here is my function call in my class file circle.cc
void circle::populate_classobj(int cx1, int cx2, int cy1, int cy1)
{
x1=cx1;
x2=cx2;
y1=cy1;
y2=cy2;
}
and here is what i actually call in main
mycircle.populate_classobj(x1,x2,y1,y2);
there are variables called x1, x2, y1, y2 in main
The really odd thing is that the redefinition error is only for cy1, not cx1, cx2 or cy2
Thanks for any help and if you need to see more of my code, ask for it.
-Will
Last two parameters are exactly same as shown below. Hence the redefinition error.
void circle::populate_classobj(int cx1, int cx2, int cy1, int cy1)
^^^ ^^^
I think you wanted to write:
void circle::populate_classobj(int cx1, int cx2, int cy1, int cy2)
void circle::populate_classobj(int cx1, int cx2, int cy1, int cy1)
You see there the redefinition of cy1, since both last arguments are called the same. By the way, names beginning with an underscore in the global namespace are reserved for the implementation, you should drop the leading underscore from your scope guards.
void circle::populate_classobj(int cx1, int cx2, int cy1, int cy1)
// ^^^ ^^^
Is this a question typo, or do you really have two parameters in the function definition named cy1?
Your definition of populate_classobj uses the same name cy1 for two different parameters.