My class isn't defined in the scope? - c++

I'm just starting to learn C++ as an already-experienced programmer in several other languages. The issue I'm having is probably a very obvious error on my end.
I have a class called Test in its own file, and it also has the respective header file. However, when I try to create an instance of it in main, I get this error:
Error: Test was not declared in this scope.
Error: Expected ';' before 'to'
This is my main:
#include <iostream>
using namespace std;
int main()
{
Test to;
return 0;
}
This is the Header for Test:
#ifndef TEST_H
#define TEST_H
class Test
{
public:
Test();
};
#endif // TEST_H
This is the Test Class:
#include "Test.h"
#include <iostream>
using namespace std;
Test::Test()
{
}
As you can see it is a simple class with an empty constructor. I have no idea what I could possibly be doing wrong here, so any help is much appreciated.
Edit: Thanks for the help, I knew it would be something stupidly obvious :P

#include "Test.h"
in your main file also.

You forgot to include the header containing the Test class so the main can "see" it. Try this code instead:
#include <iostream>
#include "Test.h"
int main()
{
Test to;
return 0;
}

Related

How do I solve this error: include files are nested too deeply

Should I put also my headers, and not only the classes inside the ifndef, define etc?
For example I have, this is just an example of code. Now, my question is, should I put the #include "myfile.h" in the ifndef, or should it stay outside?
#include <iostream>
#include "myfile.h"
#ifndef ANOTHERFILE_H
#define ANOTHERFILE_H
struct node
{
int val;
node*next;
}
#endif //ANOTHERFILE_H

Need assistance with code where it claims code not declared in this scope (C++)

I am practicing using multiple files for C++ in Code::Blocks. I have three files, two source files named main.cpp and Cat.cpp, and a header file named Cat.h. Though I declare a function designed to output text in Cat.h, the implementation in the main function returns the error "'speak' was not declared in this scope."
I tried researching the error, but that was tricky because it's such a general error that can occur for a wide variety of reasons. I tried carefully checking for syntax errors or improper #include statements in my code, but I can't find anything.
This is in my main.cpp file:
#include <iostream>
#include "Cat.cpp"
#include "Cat.h"
using namespace std;
int main()
{
speak();
return 0;
}
this is my Cat.h file:
#ifndef CAT_H_INCLUDED
#define CAT_H_INCLUDED
void speak();
#endif
and this is my Cat.cpp file:
#include <iostream>
#include "Cat.h"
using namespace std;
void speak(){
cout << "Meow!!" << endl;
}
I am expecting speak() to run, but the error says it is not declared in this scope.

C++ undefined reference to class (1 header 2 cpp's)

I am reading a book (C++ for dummies) as well as watching youtube videos to learn how to code. I am currently struggling with very simple class functions.
Main.cpp
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include "Test.h"
using namespace std;
int x;
int main(int nNumberofArgs, char* pszArgs[])
{
combat fight;
cout << x;
fight.dodmg();
cout << x;
return 0;
}
Test.h my header file with the class
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
int dodmg();
void zero_out();
private:
int x;
};
#endif // TEST_H_INCLUDED
Test.cpp class functions
#include "Test.h"
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20
}
I tried to make this very simplistic just to figure out how to work a class.
I included a lot of #includes just to try and make sure it wasn't something stupid like I needed strings.
I am not sure why but the videos I watched simply had the header say
ifndef TEST_H (of their respective code, mine has an _INCLUDE as well, I tried deleting it and it still didn't work.
My unfortunate errors
on line 14 of main.cpp fight.dodmg(); it says
\Beginning_Programming-CPP\Playing_with_class\main.cpp|14|undefined reference to `combat::dodmg()'|
then below that
||error: ld returned 1 exit status|
How are you compiling this? I think this is an issue because you arent compiling your Test.cpp file. If you arent already, try compiling with the command:
g++ main.cpp Test.cpp -o MyProgram
UPDATE:
Few things, you dont have a closing statement to your #ifndef directive in Text.h, you will need a constructor to set the value of x so i added one to the combat class also you were missing a semicolon in the zero_out function. I added comments to all the lines I changed.
Okay try this:
Test.h
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class combat
{
public:
combat(); // added constructor
int dodmg();
void zero_out();
private:
int x;
};
#endif // closed #ifndef
Text.cpp
#include "Test.h"
combat::combat() // implemented constructor
{
x = 20;
}
int combat::dodmg()
{
x = x - 5;
return x;
}
void combat::zero_out()
{
x = 20; // added ';'
}
Hope this helps,
Final edit: I dont think you really need your header guards in this scenario, you could remove the "#ifndef, #define, and the #endif" lines and not see a difference really
It sounds like you provide the wrong arguments for the compiler. Your header file (Test.h) simply provides signatures for the methods, but the implementations are given in the source file (Test.cpp).
This is an important part of writing C++ (or C) code. Your compiler does not automatically search for source files, so you need to tell it where to look, e.g.:
g++ -std=c++11 main.cpp Test.cpp -o main

Why are my functions undefined when I declared the type already?

Hi I was just trying to learn separate Classes in C++. I don't know why my code is not working.
So here is the main file code
#include <iostream>
#include "Number.h"
using namespace std;
int main()
{
Number key;
key.setNumber(200);
cout<<key.getNumber();
return 0;
}
Here is the Class cpp functions file code
#include "Number.h"
#include <iostream>
using namespace std;
void Number::setNumber(int transfernumber)
{
privatenumber = transfernumber;
}
int Number::getNumber()
{
return privatenumber;
}
And here is the header file
#ifndef NUMBER_H
#define NUMBER_H
class Number
{
public:
Number();
void setNumber(int transfernumber);
int getNumber();
private:
int privatenumber;
};
#endif // NUMBER_H
Thanks
In your cpp file you need to define the default constructor for the Number class. For example:
Number::Number() : privatenumber(0) {}
I have test your example. The error happened for the main.cpp cannot found the number.cpp. You have three ways to solve it:
write your main() to the number.cpp, not a solo file.
complie the main.cpp with the linux command gcc or write a Makefile, instead of using codeblocks.
If you want to use the codeblocks for compiling, you should create a project, and then add your three files to the project. Now compile the main.cpp.
Use the three ways above, I think you will compile successfully.
BTW, you should add the Number::Number() 's implementation.

Object creation error

I am a complete c++ noob. Start to learn it from the java. So, after hard study with some tutorials, I ended up with this class named Token:
#include "Token.h"
#include <iostream>
using namespace std;
//int Token::frequency = 0;
Token::Token() {
// TODO Auto-generated constructor stub
frequency=0;
tok = "hey i am created";
cout << tok << endl; // prints !!!Hello World!!!
}
Token::~Token() {
// TODO Auto-generated destructor stub
}
The header for this class is this:
#ifndef TOKEN_H_
#define TOKEN_H_
#include <string>
class Token {
std::string tok;
int frequency;
public:
Token();
virtual ~Token();
};
#endif /* TOKEN_H_ */
It looks like in the tutorial, all right. The error is when I called it in my main class:
#ifndef TOKEN_H_
#define TOKEN_H_
#include <iostream>
using namespace std;
int main() {
Token myToken;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
#endif /* TOKEN_H_ */
The error says :
Tokenizer.cpp:15:8: error: expected ‘;’ before ‘myToken’
Whyyy? I looked up the tutorials and answers here, it looks like I am doing it right? What is going on? And, if you see that I did some other crap, pls tell me, I will be thankful, I just came from Java to C++...
You have #ifndef TOKEN_H_ around your main function, which makes no sense. That #ifndef is called a header guard, and it's used to protected headers from being included multiple times. Your main function should go into a source file (.c or .cpp or the like). You do not need header guards in source files since they aren't included from other source files.
You need your main.cpp file to be like this:
#include <iostream>
#include "token.h"
// other stuff, like the using namespace
int main() { /* then your main function. */ }
Actually, most of your source files will follow this sort of pattern. This is the same pattern your Token.cpp file is built with.
You need to include the actual header for your class
#include "Token.h"
That way the class definition is available and you can declare an instance of Token in main
These are header/include guards, you don't need them in your main file
#ifndef TOKEN_H_
#define TOKEN_H_
You are not incluiding the token header #include "token.h", thus, the compiler think that Token myToken; is a definition not a declaration, also you don't need the header guards
remove the #ifndef guards from the main.c file. Also include the Token.h header in main.