I'm working on Blake algorithm in Visual but I have small problem.
My Block.h file
#pragma once
#include<string>
#include<bitset>
#include<iostream> // DEBUG
#include"BlocksContainer.h"
using namespace std;
class Block {
public:
static void CreateBlocks(string);
static string CreatePadding(int);
Block(string);
protected:
string BlockContent;
};
My BlocksContainer.h file
#pragma once
#include"Block.h"
class BlocksContainer {
public:
int GetLength(void);
Block* GetNBlock(int);
BlocksContainer(Block**, int);
protected:
Block** Blocks;
int Length;
};
I don't know why but Visual throw me blockscontainer.h(7): error C2143: syntax error : missing ';' before '*'
I'm newby in C++ and I can't find error. In Stack I found solutions like missing ; after class declaration, but I have semicolons.
You dont need:
#include"BlocksContainer.h"
inside block.h, this line causes Block to be undefined inside BlocksContainer.h because it was not yet visible to compiler.
In case you really need such inter dependent headers you can declare class like this:
class Block;
after such statement you are allowed to use Block class but only in compound statements like pointers or references - that means Block* GetNBlock(int); would compile.
Related
I'm working on an interesting problem.
Customer has code that builds in VS2013, they had a proliferation of macros, and in one case I couldn't understand why. Visual Studio 2013 Platform toolset V120xp.
template< class J, int a_iSize = 10 >
struct testme {
private:
vector< J > data;
};
The above code throws the error:
1>c:\customers\xxx.h(334): error C2143: syntax error : missing ';' before '<'
However, if they change all of their USES of the struct testme by wrapping it with a macro as in:
#define TEST_ME testme
The code compiles fine.
I'm a newbie to templates and wonder if there is something base level that I'm missing.
I generally understand the concept of templates, but this is an interesting nuance.
I need to dig down and create a smaller test case. At the moment, the above code snippet is part of a larger block of code, so there could be something in the call that is causing this.
Also, I could run the 'C' preprocesor only and compare the two *.i files. This may show up something.
The customer wrapping the use cases of the template function with a macro is interesting. When I see this, I think "There is something missing, that if done, could eliminate the use of a macro." That's the thing I'm looking for.
For this code:
template< class J, int a_iSize = 10 >
struct testme {
private:
vector< J > data;
};
int main()
{
testme<int> t;
}
The following error:
error C2143: syntax error : missing ';' before '<
is duplicated here.
What is probably occurring is the lack of specifying that vector is in the std namespace.
This is why header files should use the namespace name, and not rely on some outside file to provide this name.
In addition, the <vector> header should be included in the file.
#include <vector>
template< class J, int a_iSize = 10 >
struct testme {
private:
std::vector< J > data;
};
int main()
{
testme<int> t;
}
The following compiles with no errors.
I've been looking into Syntax Error C2061 for a while now, and I have come to understand that it is often caused by circular dependencies of header files. However, I believe I should've resolved this in my files yet I continue to have the issue.
Arc.h
#pragma once
#include <string>
using namespace std;
class Node;
class Arc
{
public:
Arc(Node &p_destination, const string &p_mode);
~Arc();
private:
string m_mode;
Node* m_destination;
};
Node.h
#pragma once
#include <string>
#include <vector>
using namespace std;
class Arc;
class Node
{
public:
Node(const string &p_name, const int &p_identifier, const float &p_latitude, const float &p_longitude);
~Node();
void set_arcs(Arc* p_arc) { m_arcs.push_back(p_arc); } //Line that causes the error
private:
std::vector<Arc*> m_arcs;
//Other Private Variables removed
};
The header files have both been included in the corresponding cpp files. Any help on this matter will be greatly appreciated!
Edit: Full Error Message below
"Syntax Error: identifier 'Arc'"
The problem is that the name "Arc" is already in use by a method in the global namespace. Either rename your class to an unused name or place it in a namespace which is not the global namespace.
You have a circular dependecy in you files. Arc depends on Node and Node depends on Arx. This cannot work, because you must include Arc in Node and also Node in Arc.
Forward declaration helps here a little bit but you put a using inside the header file. You shouldn't do that because then your Node and Arc is inside std. Look here for further clarification.
"using namespace" in c++ headers
I am getting this error when ever i run this project
6 error C2065: 'Engine_in':undeclared identifier
I really dont know what i have done wrong. Usually i can figure it out and know what i did wrong but the books i have dont go into depth on seperate file classes. I honestly do not know where the error is coming from. I have googled it but everyones problems are specific, so that is why i am resorting to asking you to solve my problems. I appologize in advance for me not knowing much.
I have this class 'Engine_debug.cpp'
//Engine Debugger
#include<iostream>
#include "Engine_debug.h"
#include "Engine_in.h"
using namespace std;
Engine_debug::Engine_debug()
{
Engine_in input;
}
Then i have this header 'engine_debug.h'
#ifndef Engine_debug_H
#define Engine_debug_H
class Engine_debug
{
public:
Engine_debug();
protected:
private:
}
#endif
I also have this class 'Engine_in.cpp'
//Engine input
#include<iostream>
#include<string>
#include "Engine_in.h"
using namespace std;
Engine_in::Engine_in()
{
}
string askYN(string question, int format)
{...working code}
And one more, the other header 'Engine_in.h'
#ifndef Engine_in_H
#define Engine_in_H
class Engine_in
{
public:
Engine_in();
std::string askYN(std::string question, int format = 0);
protected:
private:
};
#endif
If anyone knows what i did wrong and would like to explain to me, please do, thanks.
If it isn't a typo, you forgot to write class name while defining the member function.
string Engine_in::askYN(string question, int format)
// ^^^^^^^^^^ Missed during member function definition
Not sure if that causes the kind of error message the compiler is complaining about.
There is also a missing ; at the end of Engine_debug class definition. Credits Jesse.
I want to create a sea battle game. I have two classes: Ship and Cell.
#pragma once
#include"stdafx.h"
#include"Globals.h"
#include<vector>
#include"MCell.h"
class Ship
{
private:
int lenght;
int oriantation;
vector<Cell*> cells;
vector<Cell*> aroundCells;
...
#pragma once
#include<vector>
#include"MShip.h"
class Cell
{
private:
bool haveShip;
bool selected;
bool around;
int x;
int y;
Ship* ship;
And i have got many error like those:
1>projects\seewar\seewar\mship.h(13): error C2065: 'Cell' : undeclared identifier
1>projects\seewar\seewar\mship.h(13): error C2059: syntax error : '>'
1>projects\seewar\seewar\mship.h(14): error C2065: 'Cell' : undeclared identifier
What's wrong with the code?
Well your problem lies that when you include MCell.h you include MShip.h which references Cell defined in MCell.h. However MShip.h refers to MCell.h which won't get included because of the pragma once. If the pragma once wasn't there then you'd get an inifinite loop that would stack overflow your compiler ...
Instead you could use a forward declaration.
ie remove the #include "MCell.h" from MShip.h and replace it with, simply, "class Cell;" All your circular references issues will this go away :)
Your header files depend on each other. However one of them will have to be read before the other. So you need to rewrite one of them (or both of them) to not depend on the other. You can do that by forward-declaring the classes instead of including the header file that defines them.
So in MShip.h you should put a declaration class Cell; instead of including MCell.h and/or vice versa.
You need to forward declare the classes.
Ship.h
class Cell; //forward declaration
class Ship
{
//....
};
Cell.h
class Ship; //forward declaration
class Cell
{
//....
};
and remove the circular inclusion. You don't need to include one header in another since you're not working with complete types, but pointers. Full class definition is required when you use concrete objects of the type. In the case of pointers, you don't.
The following for creating a Global Object is resulting in compilation errors.
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
#pragma hdrstop
class Tester;
void input();
class Tester
{
static int number = 5;
public:
Tester(){};
~Tester(){};
void setNumber(int newNumber)
{
number = newNumber;
}
int getNumber()
{
return number;
}
}
Tester testerObject;
void main(void)
{
cout << "Welcome!" << endl;
while(1)
{
input();
}
}
void input()
{
int newNumber = 0;
cout << "The current number is " << testerObject.getNumber();
cout << "Change number to: ";
cin >> newNumber;
cout << endl;
testerObject.setNumber(newNumber);
cout << "The number has been changed to " << testerObject.getNumber() << endl;
}
Here are the compile errors:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class
1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'
1>.\test.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\test.cpp(49) : error C2039: 'getNumber' : is not a member of 'System::Int32'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\test.cpp(55) : error C2039: 'setNumber' : is not a member of 'System::Int32'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\test.cpp(57) : error C2039: 'getNumber' : is not a member of 'System::Int32'
1> c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>Build log was saved at "file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm"
1>test - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
How do I create a Global Class
Object correctly like I've attempted
here.
And how do I fix that "only static
const integral data members can be
initialized within a class"
And basically how do I fix the rest
of the errors so I can get this to
compile?
I like declaring Global Class Objects at file scope (I like declaring all globals at file scope) because when I have to create separate source files and do "extern" and everything it becomes extremely complicated and never works for me. Although, I do want to figure out how to do that eventually... it seems every tutorial I look at won't compile though and unless it compiles I have no idea how to recreate it!
If I can just get this to compile...then I can successfully learn how to do this. So if someone could rewrite the above to where it literally copies & pastes into Visual C++ Express 2008 and works I will finally be able to figure out how to recreate it. I'm extremely excited on seeing the fix for this! It is just I can't get Global Objects to work right! Any other information on declaring Global Class Objects...or anything for that matter is welcome!
Just start addressing the errors one by one. A lot of the errors are just cascaded from the initial errors, so it looks like there are a lot of problems when there's only a couple. Just start from the top:
1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class
You can't initialize a member in the class definition unless it's static, const, and one of the integral types. Leave the "= 5" off of the declaration of number. Then you'll need to have a definition of Tester::number outside of the class definition, like so:
int Tester::number = 5;
Problem #2:
1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'
Almost exactly what it says (missing semi-colon errors can be a bit inexact in saying where the semicolon should be) - you need a semi-colon after the definition of the Tester class.
Fix those and your compilation problems go away.
The key thing is to try and take compiler errors one at a time from the top. If you get more than about 3 of them, you can probably just ignore everything after the 3rd or so because the initial error just cause the compile to into the weeds (and if they are real errors, they'll show up again in the next compile anyway).
Error C2864: either add a const modifier to your integer, or move the initialization outside the class (as in class Tester { static int number; }; int Tester::number = 5;). The latter seems more appropriate to your case.
Error C2146: you're missing a semicolon after the declaration of class Tester { ... }. It should be class Tester { ... };
The other errors are probably caused by the previous error. They should fix themselves automatically when it is fixed.
As a side note, I don't think you really want the static modifier on your member. It seems more appropriate for an instance field. You still can't initialize it in-place though (this isn't C#), you have to move the initialization to the constructor. For example:
class Tester {
int number;
static int staticNumber; // just to show you how to use a static field
public:
Tester() : number(5) {}
~Tester() {} // I suggest you remove the destructor unless you need it
int getNumber() { return number; }
void setNumber(int value) { number = value; }
static int getStaticNumber() { return staticNumber; }
static void setStaticNumber(int value) { staticNumber = value; }
};
// initialize static members *outside* the class
int Tester::staticNumber = 5;
According to this: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr038.htm
Tester testerObject;
int Tester::number = 5;
I'm not positive, but I think the rest of the errors come from that one problem.
Fix that, and see how far it gets you.
the answers already here deal with why your code doesn't compile and how to correct that. however i am intrigued by your comments about "extern". it is very easy to use when you know how. you declare in one header the extern'ed variable. and then you initialise it in one file. any other file can refer to the variable by including the header. e.g.
header.h:
// ensure the file is only included once
#ifndef _HEADER_H
#define _HEADER_H
extern int foo;
#endif
// end file header.h
header.cpp
#include "header.h"
int foo = 1;
// end file header.cpp
main.cpp
#include "header.h"
#include <stdio.h>
int main(int argc, char** argv)
{
printf("%d", foo);
return 0;
}
// end file main.cpp
Whilst using static class members for global variables helps fit the oo design scheme, its more elaborate than necessary. if you don't have to follow oo strictly, just use extern, its easier and its less code.