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.
Related
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.
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;
}
I'm working on making a game in C++. I have declared a Constant namespace used for global values that I need access to throughout the program. In there I have an ofstream for debugging purposes (yeah, I know it's not "constant" but it fits best there), which outputs only when it feels like it. I was able to make a small program demonstrating the problem. I apologize for it being spread across 4 files, but it is important, I promise.
main.cpp:
// Include necessary files
#include "test.h"
#include "constants.h"
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
// Start of program
Constant::outstream.open("test.txt");
// ...
// Do stuff
// Output debugging info
Test test;
test.print("Test", Constant::outstream);
// ...
// Do other stuff
// End of program
Constant::outstream.close();
return 0;
}
constants.h:
#ifndef _CONSTANTS_H
#define _CONSTANTS_H
#include <fstream>
namespace Constant
{
static ofstream outstream;
}
#endif
test.h:
#ifndef _TEST_H
#define _TEST_H
#include <string>
#include <fstream>
#include "constants.h"
class Test
{
public:
void print(string str, ofstream& out);
};
#endif
test.cpp:
#include "test.h"
using namespace std;
void Test::print(string str, ofstream& out)
{
out << "out: " << str << endl << flush; // Works
Constant::outstream << "Constant::outstream: " << str << endl << flush; // Doesn't
}
In the test.cpp file, the out << ... line works as it should, while the Constant::outsream << ... line doesn't do anything even though I'm passing Constant::outstream as the out parameter! I don't see any reason why these two lines should be in any way different.
Before posting this, I tried putting test.cpp's code in test.h, just to have less files for the question, and was amazed to see it work. If I copy-paste the Test::print() function into test.h (whether inside or out of the class Test { ... }), then both output commands work correctly. the problem only occurs if Test::print()'s implementation is in a separate file.
It seems like any references to Constant::outstream simply don't work in class cpp files (no compile error, just nothing happens). It works in main.cpp and in class header files, but any class cpp file it seems not to. Unfortunately, this is a big program I'm writing so pretty much every class has its own cpp implementation file, and that's really the one place I need to use this ofstream. Does anyone know the reason for this?
Thanks in advance,
Doug
Constant::outstream has internal linkage, thus a separate instance is created for each translation unit. In short, Constant::outstream in test.cpp and main.cpp are two different variables.
§3.5.2 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a variable, function or function template that is explicitly declared static; or,
On the other hand, static class members would be visible throughout the program.
So, if you would write
struct Constant
{
static ofstream outstream;
}
instead of
namespace Constant
{
static ofstream outstream;
}
it would work.
However, note that the class must have external linkage; e.g. you should not put in in anonymous namespace.
I don't know why I can't access the function clearConsole() from my .cpp file from the header files, I guess I'm calling it wrong? How do I target the main file from a header file? I try to call the clearConsole() function after the user input in the addCustomer() functinon in customer.h.
Main.cpp
// OTS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#include "customer.h"
// Clear function specific to Windows
// Cross platform alternatives are more convoluted to reach desired effect, so have not been included
void clearConsole()
{
#ifdef _WIN32
system("cls");
#endif
}
Customer.h
//customer.H
//The object class customer
class customer
{
//...
clearConsole();
}
If your files are linked together, a forward declaration of the functions should be enough.
Customer.h
//customer.H
//The object class customer
void clearConsole(); // <--- declare function
class customer
{
//....
};
But this structure looks wrong. I would declare the function in a different header, inside a namespace, and define it in a corresponding implementation file:
clearconsole.h
namespace ConsoleUtils
{
void clearConsole();
}
clearconsole.cpp
namespace ConsoleUtils
{
void clearConsole()
{
}
}
Move your clearConsole() method to the header file (I think is not under discussion the implementation under .header files that I actually disagree, but anyway...), and change the system message to the specific one you need, as follows:
#ifndef _WIN32
#include <syscall.h>
#endif
void clearConsole(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
I also had this problem in my kernel that I'm writing in C,C++, and Assembly. I was able to fix this problem by telling the ld command to allow shared variables and functions using the -shared flag. In gcc you would just do the same thing because gcc is a linker, assembly, c compiler and a c++ compiler.
Its the first time I am trying to separate the class in a separate header file but I am getting an error.Please help me out.Thanks
CODE:
My main function:
#include <iostream>
#include <MyClass>
int MyClass::data;
int main()
{
cout<<"data="<<MyClass::data;
system("pause");
return 0;
}
MyClass.h
#ifndef MyClass
#define <MyClass>
class MyClass
{
static int data_;
};
#endif
Error: fatal error C1083: Cannot open include file: 'MyClass.h': No such file or directory
You should use
#include "MyClass.h"
angle brackets are for system headers.
Also it's data or data_?
Also it would be better something like
#if !defined(MYCLASS_H_INCLUDED)
#define MYCLASS_H_INCLUDED
...
#endif
#define-ing a name identical to the class name is going to be a source of problems
First good idea to separate definition and implementation in C++. Your #include directive shall use " and not < > as your header is not a system header. Or your header is not lying inside the same directory than the cpp file.
That is another topic but OO is more than just using some classes. Encapsulating static variables inside a class doesn't make them less global... At least they have another namespace...
use #include "Myclass.h" instead of #include