I just recently started messing around with separate class files in c++ and this was my first attempt:
First I made a class header called "ThisClass.h":
//ThisClass.h
#ifndef THISCLASS_H
#define THISCLASS_H
class ThisClass
{
private:
int x;
float y;
public:
ThisClass(int x, float y);
void setValues(int x, float y);
int printX();
float printY();
};
#endif // THISCLASS_H
Then, I implemented my class in a file called "ThisClass.cpp":
//ThisClass.cpp
#include "ThisClass.h"
ThisClass::ThisClass(int x, float y)
{
this->x = x;
this->y = y;
}
void ThisClass::setValues(int x, float y)
{
this->x = x;
this->y = y;
}
int ThisClass::printX()
{
return this->x;
}
float ThisClass::printY()
{
return this->y;
}
Finally, I made a file called "main.cpp" where I used the class:
//main.cpp
#include <iostream>
using namespace std;
int main()
{
ThisClass thing(3, 5.5);
cout << thing.printX() << " " << thing.printY()<< endl;
thing.setValues(5,3.3);
cout << thing.printX() << " " << thing.printY()<< endl;
return 0;
}
I then compiled and ran this program through Code Blocks which uses the MinGW compiler and received the following errors:
In function 'int main()':|
main.cpp|7|error: 'ThisClass' was not declared in this scope|
main.cpp|7|error: expected ';' before 'thing'|
main.cpp|8|error: 'thing' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Am I somehow doing this wrong? Any help would be appreciated.
You forgot to #include "ThisClass.h" in main.cpp.
As already answered that you forgot to put #include "ThisClass.h"in main.cpp.
Just do that and your code will be compiled.
I just want to answer your question -
However, now my console isn't outputting anything even though I have 2 cout calls
Please put a getchar() before return in main function, it will allow you to see your output.
Related
i used struct in aheader file and i included it in the main.cpp and in the class.h and it's cpp but i got some errors the errors are
Error 1 error C2511: 'void fkingatm::test1(user [])' : overloaded member function not found in 'fkingatm'
Error 2 error C3861: 'test1': identifier not found
Error 3 IntelliSense: identifier "test1" is undefined
STRUCT_HEADER.h
#ifndef STRUCT_HEADER
#define STRUCT_HEADER
struct user {
int arr[10];
};
#endif
main.cpp
#include "STRUCTHEADER.h"
int main(){
user users[10];
test1(users);
fkingatm.h (the class file)
#ifndef FKINGATM_H
#define FKINGATM_H
#include "STRUCTHEADER.h"
class fkingatm
{
public:
void test1(user);
};
#endif
fkingatm.cpp
#include "fkingatm.h"
void fkingatm::test1(user users[10]){
for (int i = 0; i < 10; i++){
cin >> users[0].arr[i];
}
for (int i = 0; i < 10; i++){
cout<< users[0].arr[i]<<endl;
}
}
Your main.cpp evaluates as: (Assuming STRUCT_HEADER isn't #defined before STRUCT_HEADER.h and that you just misspelled your STRUCTHEADER.h in this question )
struct user {
int arr[10];
};
int main(){
user users[10];
test1(users);
Since #include only pastes whatever the contents of the file you put in front of it on the line it's written on. It won't see your definition of test1.
You should try:
#include "fkingatm.h"
#include "STRUCTHEADER.h"
int main(){
user users[10];
fkingatm f;
f.test1(users);
return 0;
}
Now, that solves the 2nd and 3rd Error you had. For the 1st error, you will need to edit the declaration of your void
void test1(user);
to
void test1(user[10]);
edit:
That is because you're using your test1(user user[10]) as one that takes a user[10] and not a user. And user[10] is different type and data on its own. Different data in the sense that user[10] is actually a pointer(a number that represents a memory address) and user is a class/struct(Which is only a namespace and doesn't really have an exact physical representation in memory).
header file:
#ifndef CONTMEM_H
#define CONTMEM_H
class Contmem {
public:
Contmem(int a, int b, int c);
int total;
int mem;
const int constmem;
int printconst() const;
const int constant;
void print();
};
#endif // CONTMEM_H
Contmem.cpp file:
#include "Contmem.h"
#include <iostream>
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
{
total = mem * constmem * constant;
}
void Contmem::print()
{
std::cout << "This is my variable member " << mem << " and this is my constmem member " << constmem << "with the constant member" << constant << std::endl;
}
int Contmem::printconst() const
{
return total;
std::cout << "This is the total variable " << total << std::endl;
}
main function :
#include <iostream>
#include "Contmem.h"
int main()
{
Contmem cont(3, 4, 5);
cont.print();
const Contmem obj;
obj.printconst();
}
error file:
|=== Build: Debug in CONST_&_MEMBER_INITIALIZER
(compiler: GNU GCC Compiler) ===| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\main.cpp||
In function 'int main()':| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\main.cpp|9|error:
no matching function for call to 'Contmem::Contmem()'|
C:\C++ CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|8|note: candidate:
Contmem::Contmem(int, int, int)| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|8|note: candidate
expects 3 arguments, 0 provided| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|5|note: candidate:
Contmem::Contmem(const Contmem&)| C:\C++
CODEBLOCK\CONST_&_MEMBER_INITIALIZER\Contmem.h|5|note: candidate
expects 1 argument, 0 provided| ||=== Build failed: 1 error(s), 0
warning(s) (0 minute(s), 0 second(s)) ===|
You're missing a default constructor for your class. You only have this one
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
{
total = mem * constmem * constant;
}
but here
int main()
{
Contmem cont(3, 4, 5);
cont.print();
const Contmem obj; // <--here
obj.printconst();
}
you are trying to construct a new Contmem object without passing those 3 arguments
Actually, those compiler errors are telling you what the real problem is.
const Contmem obj;
attempts to call the default constructor, Contmem().
BUT because of:
Contmem::Contmem(int a, int b, int c)
: mem(a), constmem(b), constant(c)
which has a mem-initializer and a const member, your default constructor is deleted.
Thus, compiler can't match that statement to any of the existing constructor, since only your mem-initializer constructor exists.
I'm writing a c++ stack and queue implementation program, I finished the stack part, but when compiling I'm getting these errors
arrayListImp.cpp:18:19: error: expected unqualified-id
arrayList[++top]= x;
^
arrayListImp.cpp:28:13: error: 'arrayList' does not refer to a value
itemPoped=arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:35:9: error: 'arrayList' does not refer to a value
return arrayList[top];
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
arrayListImp.cpp:46:9: error: 'arrayList' does not refer to a value
cout<<arrayList[i]<<endl;
^
./arrayList.h:3:7: note: declared here
class arrayList{
^
4 errors generated.
Here is the header file
#ifndef ARRAYLIST_H
class arrayList{
public:
arrayList();
static const int maxSize = 10;
int array[10];
};
class stack : public arrayList{
public:
stack();
void push(int x);
void pop();
int Top();
int isEmpty();
void print();
int x;
int top;
int itemPoped;
int i;
};
#define ARRAYLIST_H
#endif
arrayListImp.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
//Stack implementation
stack::stack(){
top = -1;
}
void stack::push(int x){
if (top == maxSize -1){
cout<<"Stack overflow"<<endl;
}
else{
arrayList[++top]= x;
cout<<x<<", is pushed on to the stack"<<endl;
}
}
void stack::pop(){
if (top == -1){
cout<<"Stack underflow"<<endl;
}
else{
itemPoped=arrayList[top];
top--;
cout<<itemPoped<<", is poped from the stack"<<endl;
}
}
int stack::Top(){
return arrayList[top];
}
int stack::isEmpty(){
if (top == -1) return 1;
return 0;
}
void stack::print(){
cout<<"Stack: "<<endl;
for (i = 0; i<=top; i++){
cout<<arrayList[i]<<endl;
}
}
arrayListUse.cpp
#include <iostream>
#include "arrayList.h"
using namespace std;
int main()
{
//Stack testing
stack S;
S.push(1);S.print();
S.push(2);S.print();
S.push(3);S.print();
S.pop();S.print();
S.push(4);S.print();
//Queue testing
return 0;
}
Can you please point out to what I'm doing wrong here?
You should just read your error messages.
You should use array instead of arrayList, which is the name of the class. So just refer to the variable instead.
The error message you got is something like
test.cpp: In member function ‘void stack::push(int)’:
test.cpp:44:18: error: expected unqualified-id before ‘[’ token
arrayList[++top]= x;
^
When you check the line, you immediately see what is wrong there.
You declare a constructor arrayList::arrayList(), but you do not define it. Either you can drop the declaration, or you should implement it in the cpp-file.
arrayList::arrayList() {
// do some initialization
}
The error message you got is something like
/tmp/cc4y06YN.o:test.cpp:function stack::stack(): error: undefined reference to 'arrayList::arrayList()'
The code could compile, but it did not link. So all declarations may be correct, but a symbol was missing. This is usually the case when you declared something you referred to, but you never defined it.
You always have written
arrayList[...]
what is the name of your class but reading the code it seems like you wanted to write
array[...]
which would access the data.
I'm learning to create classes in c++ and I've created a simply Point class. It somehow doesn't compile and I have no clue what went wrong. Please help.
Point.h
#ifndef POINT_H
#define POINT_H
class Point {
private:
float x, y;
public:
//default constructor
Point();
//constructor
Point(float x, float y);
float getX();
float getY();
void print();
};
#endif
Point.cpp
#include "Point.h"
Point::Point(){
x = 0.0;
y = 0.0;
};
Point::Point(float x, float y){
x = x;
y = y;
}
float Point::getX(){
return x;
}
float Point::getY(){
return y;
}
void Point::print(){
cout << "hello" ;
{
main.cpp:
#include <Point.h>
#include <iostream>
int main()
{
Point p(10.0f, 20.0f);
p.print();
return 0;
}
Below is the build message:
||=== Build: Debug in Point (compiler: GNU GCC Compiler) ===|
main.cpp|7|error: no matching function for call to 'Point::Point(float, float)'|
main.cpp|8|error: 'class Point' has no member named 'print'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
You forgot to put Point:: in front of print when defining the body. Also, the x = x in the constructor won't do anything. You need to assign to this->x, and likewise for y.
always use Constructor initialize list if possible
Point::Point()
: x(0.f)
, y(0.f)
{
}
Point::Point(float x, float y)
: x(x)
, y(y)
{
}
return const for both getX() getY()
Apologies for the code dump:
gameObject.cpp:
#include "gameObject.h"
class gameObject
{
private:
int x;
int y;
public:
gameObject()
{
x = 0;
y = 0;
}
gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
~gameObject()
{
//
}
int add()
{
return x+y;
}
};
gameObject.h:
class gameObject
{
private:
int x;
int y;
public:
gameObject();
gameObject(int inx, int iny);
~gameObject();
int add();
};
Errors:
||=== terrac, Debug ===|
C:\terrac\gameObject.cpp|4|error: redefinition of `class gameObject'|
C:\terrac\gameObject.h|3|error: previous definition of `class gameObject'|
||=== Build finished: 2 errors, 0 warnings ===|
I can't figure out what's wrong. Help?
You're defining the class in the header file, include the header file into a *.cpp file and define the class a second time because the first definition is dragged into the translation unit by the header file. But only one gameObject class definition is allowed per translation unit.
You actually don't need to define the class a second time just to implement the functions. Implement the functions like this:
#include "gameObject.h"
gameObject::gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
int gameObject::add()
{
return x+y;
}
etc
add in header files
#pragma once
the implementation in the cpp file should be in the form
gameObject::gameObject()
{
x = 0;
y = 0;
}
gameObject::gameObject(int inx, int iny)
{
x = inx;
y = iny;
}
gameObject::~gameObject()
{
//
}
int gameObject::add()
{
return x+y;
}
not within a
class gameObject
{
}
definition block
You should wrap the .h file like so:
#ifndef Included_NameModel_H
#define Included_NameModel_H
// Existing code goes here
#endif
You're defining the same class twice is why.
If your intent is to implement the methods in the CPP file then do so something like this:
gameObject::gameObject()
{
x = 0;
y = 0;
}
gameObject::~gameObject()
{
//
}
int gameObject::add()
{
return x+y;
}
If you are having issues with templates or you are calling the class from another .cpp file
try using '#pragma once' in your header file.
Either try adding #pragma once at the top of your file, or the old way... Place this at the top of your code
#ifndef GAME_H //ensuring that this object is only initialized once
#define GAME_H
and this below the last line
#endif
Which will ensure only a single initialization of the class.
Include a few #ifndef name #define name #endif preprocessor that should solve your problem.
The issue is it going from the header to the function then back to the header so it is redefining the class with all the preprocessor(#include) multiple times.
You define the class gameObject in both your .cpp file and your .h file.
That is creating a redefinition error.
You should define the class, ONCE, in ONE place.
(convention says the definition is in the .h, and all the implementation is in the .cpp)
Please help us understand better, what part of the error message did you have trouble with?
The first part of the error says the class has been redefined in gameObject.cpp
The second part of the error says the previous definition is in gameObject.h.
How much clearer could the message be?