C++ error runing "hello world", vs2010 - c++

#include <iostream>
using namespace std;
int main()
{
cout<<"hello world!"<<endl;
return 0;
}
I am trying to run this simplest code in vs2010, but I received more than 100 errors when I was compiling it.
f:\vs2010\vc\include\fstream(14): error C2143: syntax error : missing ';' before '*'
f:\vs2010\vc\include\fstream(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
f:\vs2010\vc\include\fstream(16): error C2653: 'ios_base' : is not a class or namespace name
f:\vs2010\vc\include\fstream(16): error C2061: syntax error : identifier 'openmode'
f:\vs2010\vc\include\fstream(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Errors like these. What should I do?

try to include this lab in your code
#include "stdafx.h"

Related

Member redeclaration not allowed in Visual studio 2019 (same code compiled successfully in VS 2013)

While migrating from Visual studio 2013 to Visual studio 2019 compiler I have got below error. Please help me in fixing the same.
I have declared the function in the header file (.h) below:
#ifndef CSAHCCOMPOSEDITEM_H
#define CSAHCCOMPOSEDITEM_H
#ifdef _UTEST
class CsaHcDICOMComposerTester;
#endif
class EXP_IMP_HcDicComp CsaHcComposedItem
{
#ifdef _UTEST
friend class CsaHcDICOMComposerTester;
#endif
public:
enum CsaHcComposedItemType
{
CISegment,
CIPage,
CILayout,
CIPageBracket,
CIPrintJobBracket,
CIDummy
};
CsaHcComposedItem
(bool &status, CsaHcComposedItemType type_in);
CsaHcComposedItem
();
CsaHcComposedItem a
(const CsaHcComposedItem& compObj_in);
CsaHcComposedItem& operator=
(const CsaHcComposedItem& compObj_in);
~CsaHcComposedItem();
bool operator==
(const CsaHcComposedItem& ci_in);
private: // attributes
CsaHcComposedItemType
myType;
CsaHcBasicFilmSession
*myBFS;
CsaHcBasicFilmBox
*myBFB;
CsaHcBasicImageBox
*myBIB;
CsaDib *myDib;
BYTE *myPixelArray;
};
#endif // CSAHCCOMPOSEDITEM_H
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And cpp file contains the definition for the constructor.
//pusedo code
CsaHcComposedItem::CsaHcComposedItem(bool &status_out,
// Return status of the construcor
CsaHcComposedItemType type_in)
// Composed item type
: myType(type_in), // error shown for this line (70)
myBFS(NULL), //line71
myBFB(NULL),
myBIB(NULL),
myDib(NULL),
myPixelArray(NULL)
{
.....
}
Error:
1.CsaHcComposedItem.cpp(70): error C2761: '{ctor}': redeclaration of member is not allowed
2.CsaHcComposedItem.cpp(70): error C2059: syntax error: ':'
3.CsaHcComposedItem.cpp(70): error C2065: 'type_in': undeclared identifier
4.CsaHcComposedItem.cpp(70): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
5.CsaHcComposedItem.cpp(71): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
6.CsaHcComposedItem.cpp(72): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7.CsaHcComposedItem.cpp(73): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8.CsaHcComposedItem.cpp(74): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
9.CsaHcComposedItem.cpp(75): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
10.CsaHcComposedItem.cpp(78): error C2448: 'myPixelArray': function-style initializer appears to be a function definition
That source is compiled without error in VS2019(16.4.5)
I compiled with these declarations
#include <Windows.h>
#define EXP_IMP_HcDicComp
using CsaHcBasicFilmSession = int;
using CsaHcBasicFilmBox = int;
using CsaHcBasicImageBox = int;
using CsaDib = int;
Issue is fixed,
below line of code was not commented in my cpp file, after commenting it worked.

Passing template class instance to a constructor of another class

My code:
BlockyWorld.hpp
#ifndef BLOCKYWORLD_H
#define BLOCKYWORLD_H
#include <CImg.h>
namespace logic {
class BlockyWorld {
public:
BlockyWorld( const CImg<float>* heightmap );
};
}
#endif // BLOCKYWORLD_H
BlockyWorld.cpp
#include "BlockyWorld.hpp"
namespace logic {
BlockyWorld::BlockyWorld( const CImg<float>* heightmap ) {}
}
main.cpp
#include <CImg.h>
#include "logic/BlockyWorld.hpp"
//...
CImg<float> heigthMap;
logic::BlockyWorld world( &heigthMap );
//...
I get alot of errors while compiling:
main.cpp:
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
main.cpp(85): error C2664: 'logic::BlockyWorld::BlockyWorld(const logic::BlockyWorld &)' : cannot convert argument 1 from 'cimg_library::CImg<float>' to 'const int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
BlockyWorld.hpp & cpp
include\logic\blockyworld.hpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.hpp(9): error C2143: syntax error : missing ',' before '<'
include\logic\blockyworld.cpp(4): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
include\logic\blockyworld.cpp(4): error C2143: syntax error : missing ',' before '<'
I don't think it's a circular inclusion error which sometimes causes these kinds of errors for me=).
I must be defining constructor wrong or maybe I'm defining implementation wrong? Was searching for an answer for abount an hour now so I would really use an explanation now.
And just to clarify - I'm not a beginner c/c++ programmer but these templates are confusing :(
Have a nice day and thank your for your answers.
CImg appears to be part of the cimg_library namespace.
Either add using namespace cimg_library to the top of your BlockyWorld.hpp file, or change the function signature to use the namespace like so:
BlockyWorld( const cimg_library::CImg<float>* heightmap );
Along with πάντα ῥεῖ's suggestion of matching up your pointer and reference types.

Why C++ not know TIMESTAMP_STRUCT type?

I use Visual Studio 2005
When I compile, I get this error:
Error 1 error C2146: syntax error : missing ';' before identifier 'mDropEndTime'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
struct MB_SYN_DROPITEM_UPDATE : MSGBUF
{
long mCid; // Index
unsigned long mItemIdx; // idx
TIMESTAMP_STRUCT mDropEndTime; // This is error line
};
Why doesn't C++ know TIMESTAMP_STRUCT?
TIMESTAMP_STRUCT is something defined in sqlext.h
You must add
#include <sqlext.h>
Because TIMESTAMP_STRUCT is not part of the C++ standard.

getting errors while instantiating pointer for structs of native c++ code from c++/cli

below mentioned is my c++/cli "*.h" file
// rmsCInterfaceWrapper.h
#ifndef RMSCINTERFACE_H
#define RMSCINTERFACE_H
#ifndef RMSREQINFO_H
#define RMSREQINFO_H
#ifndef RMSCLIENTINFO_H
#define RMSCLIENTINFO_H
#ifndef RMSPHYSICIANINFO_H
#define RMSPHYSICIANINFO_H
#ifndef RMSREQPOLICYINFO_H
#define RMSREQPOLICYINFO_H
#pragma once
#include "D:\nbsource code\RMS\rmsCAPI\rmsCInterface.h"
#pragma comment(lib,"rmsCAPI.lib")
#include "D:\nbsource code\RMS\rmsDLL\rmsReqInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsClientInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsPhysicianInfo.h"
#include "D:\nbsource code\RMS\rmsDLL\rmsReqPolicyInfo.h"
#pragma comment(lib,"rmsDLL.lib")
using namespace System;
namespace rmsCInterfaceWrapper
{
public ref class rmsCInterface
{
// TODO: Add your methods for this class here.
private:
rmsReqInfoStruct *rmsReqInfo;
rmsClientInfoStruct *rmsClientInfo;
rmsPhysicianInfoStruct *rmsPhysicianInfo;
rmsReqPolicyInfoStruct *rmsReqPolInfo;
rmsAddlOrderInfoStruct *rmsAddOrderInfo;
public:
rmsCInterface();
~rmsCInterface();
long OrderReq();
};
}
#endif
i have declared pointer for these native c++ structures rmsReqInfoStruct, rmsClientInfoStruct, rmsPhysicianInfoStruct, rmsReqPolicyInfoStruct, rmsAddlOrderInfoStruct. wheni compile this i am getting these below mentioned errors and i am not sure why i am getting those
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(30) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(31) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C2143: syntax error : missing ';' before '*'
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\vamsi\rmscinterfacewrapper\rmscinterfacewrapper\rmsCInterfaceWrapper.h(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
All these errors are showing up at the structure pointers in the class private declaration itself and i am unable to figure them out why they are causing. can any one please help.
I was able to solve the issue. As i have created a clr--->class library project it automatically generated the stdafx.h and stdafx.cpp files. As i am writing a wrapper around my native c++ code using c++/cli i copied the auto generated c++/cli stdafx.h file with my native stdafx.h file and removed the #ifndef,#define,#pragma statements from the .h file that i have provided above, so doing all these it worked, and i was able to generate the dll file which i can use in my c# program as a reference... Thank you every one for your help...

How to use circularly dependent classes in C++?

This code is not compiling.
What modification can I do to achieve the desired result?
ClassOne.h
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
#include "ClassTwo.h"
class ClassTwo;
class ClassOne
{
private:
string message;
friend ClassTwo;
ClassTwo m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
ClassTwo.h
#ifndef _CLASS_TWO_
#define _CLASS_TWO_
#include <string>
#include "ClassOne.h"
class ClassOne;
class ClassTwo
{
private:
string message;
friend ClassOne;
ClassOne m_ClassOne;
public:
ClassTwo();
void Display();
};
#endif
ClassOne.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
#include <iostream>
ClassOne :: ClassOne()
{
std::cout<<"ClassOne()...called\n";
this->m_ClassTwo.message = "ClassOne - Message\n";
}
void ClassOne :: Display()
{
std::cout<<this->m_ClassTwo.message;
}
ClassTwo.cpp
#include "ClassTwo.h"
#include "ClassOne.h"
#include <iostream>
ClassTwo :: ClassTwo()
{
std::cout<<"ClassTwo()...called\n";
this->m_ClassOne.message = "ClassTwo - Message\n";
}
void ClassTwo :: Display()
{
std::cout<<this->m_ClassOne.message;
}
main.cpp
#include "ClassOne.h"
#include "ClassTwo.h"
int main()
{
ClassOne one;
one.Display();
ClassTwo two;
two.Display();
}
Error messages
1 error C2146: syntax error : missing ';' before identifier 'message'
2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
4 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
5 error C2146: syntax error : missing ';' before identifier 'message'
6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8 error C2039: 'message' : is not a member of 'ClassTwo'
9 error C2039: 'message' : is not a member of 'ClassTwo'
10 error C2146: syntax error : missing ';' before identifier 'message'
11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
13 error C2079: 'ClassOne::m_ClassTwo' uses undefined class 'ClassTwo'
14 error C2146: syntax error : missing ';' before identifier 'message'
15 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
17 error C2039: 'message' : is not a member of 'ClassOne'
18 error C2039: 'message' : is not a member of 'ClassOne'
19 error C2146: syntax error : missing ';' before identifier 'message'
20 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
21 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
22 error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
23 error C2146: syntax error : missing ';' before identifier 'message'
24 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
25 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
You cannot, ever, compile that code. You have described a system where type A contains type B, and type B contains type A. Therefore, both type A and B recursively and infinitely contain themselves, which is impossible. You must fundamentally re-architect your code to eliminate this problem.
Also, your friend syntax is wrong.
As said, you need to forward declare at least one of ClassOne or ClassTwo.
You ClassOne.h could therefor look like:
#ifndef _CLASS_ONE_
#define _CLASS_ONE_
#include <string>
class ClassTwo;
class ClassOne
{
private:
string message;
ClassTwo* m_ClassTwo;
public:
ClassOne();
void Display();
};
#endif
As you can see, we declare ClassTwo, but do not include it. We basically only tell the compiler that yes, we do have a ClassTwo, but we don't really care what it contains right now.
Also look at your ClassTwo member, this is now a pointer. The reason is that a member would require the compiler to know the size of the object, which we currently have no clue what is. Therefor you need either a pointer or a ref.
Next, in your ClassOne.cpp, would will need to include ClassTwo.h, to get the functions and size of that class.
A few things though:
With a forward declaration you can not inherit from ClassTwo, use the methods of the forwarded class in the header file (How would the compiler know which methods exists?) Define functions or methods using the forwarded class, that is you have to pass by reference or pass a pointer.
1) Use forward declarations:
add
class ClassTwo;
to ClassOne.h
and
class ClassTwo;
to ClassTwo.h
2) You need to create your member variables (or at least one of them) dynamically, using operator new, or if you wish, with one of the smart-pointers, like boost::shared_ptr from boost library or std::shared_ptr from standard library if you use C++11.