"Declaration of xxx outside of class is not definition"错误
"Declaration of xxx outside of class is not definition" error
这是错误:
错误:类外"DataStream::DataStream(("的声明不是定义[fPermission]|
||===生成完成:1个错误,0个警告(0分0秒(===|
这是主.cpp文件:
#include <iostream>
#include <iomanip>
#include "DataStream.h"
#include "MsgPacket.h"
using namespace std;
DataStream * Packet = new DataStream();
DataStream::DataStream();
int main()
{
int source;
int destination;
int type;
int port;
int input;
std::string data;
cout << "My Assignment" << endl;;
MsgPacket * Packet = new MsgPacket(source, destination, type, port, data);
}
这是MsgPacket.h
#ifndef MSGPACKET_H
#define MSGPACKET_H
#include <string>
#include "PacketAddress.h"
using namespace std;
class MsgPacket : public PacketAddress
{
public:
MsgPacket();
MsgPacket (const MsgPacket & rhs);
MsgPacket(string dataIn);
MsgPacket(int source, int destination, int port, int type, std::string data);
MsgPacket(int, char data);
string toString();
string getData() const {return _data;};
void setData(string inData) {_data = inData;};
string dataOutput();
virtual ~MsgPacket();
virtual MsgPacket * Clone() { return new MsgPacket(*this); }
protected:
string _data;
};
#endif // MSGPACKET_H
最后是MsgPacket.cpp
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
#include "msgpacket.h"
using namespace std;
MsgPacket::MsgPacket():
PacketAddress(0, 0)
{
}
MsgPacket::MsgPacket (const MsgPacket & rhs):
PacketAddress(rhs),
_data(rhs.getData())
{
}
MsgPacket::MsgPacket(string dataIn):
PacketAddress(0,0)
{
string temp;
temp = dataIn.substr (0, 4);
_source = atoi(temp.c_str());
temp = dataIn.substr (5, 4);
_dest = atoi(temp.c_str());
temp = dataIn.substr (10, 4);
_type = atoi(temp.c_str());
temp = dataIn.substr (15, 4);
_port = atoi(temp.c_str());
_data = dataIn.substr (20, dataIn.length());
#ifdef DEBUG
cout << "CREATE PACKET: " << this->toString() << endl;
#endif
}
MsgPacket::MsgPacket(int source, int destination):
PacketAddress(source, destination)
{
}
MsgPacket::MsgPacket(int source, int destination, int port):
PacketAddress(source, destination)
{
_port = port;
}
MsgPacket::MsgPacket(int source, int destination, int type, int port, std::string data):
PacketAddress(source, destination)
{
_source = source;
_dest = destination;
_type = type;
_data = data;
_port = port;
}
string MsgPacket::dataOutput()
{
stringstream output; // Create a stringstream
output << setw(4) << setfill('0') << _source << ":"
<< setw(4) << setfill('0') << _dest << ":"
<< setw(4) << setfill('0') << _type << ":"
<< setw(4) << setfill('0') << _port << ":"
<< _data;
return output.str();
}
string MsgPacket::toString()
{
stringstream output; // Create a stringstream
output << "[" << showbase << hex
<< this << "] S:["
<< _source << "] D:["
<< _dest << "] P:["
<< _type << "] T:["
<< _port << "]"
<< " DATA[" << _data << "]";
return output.str();
}
另一种可能导致此错误的方法是下面的代码。
class A
{
void a_function();
};
A::a_function(); // Note the semicolon here
{
// function contents...
}
如果你还没有摄入足够的咖啡因,那么额外的分号可能很难识别。。。
如果有人因为语法错误而来到这里,请注意,在初始化参数时,您可能会报告与语法错误相同的错误:
class A
{
A ();
char b;
};
A::A ()
, b(0) // Wrong. It should be ':' instead of ','
{
}
DataStream::DataStream();
是类DataStream
的构造函数的声明。它必须在类内声明,而不是在类外声明。
class DataStream
{
public:
DataStream();
};
此外,您可以在类内部或外部内联定义此构造函数,如
DataStream::DataStream()
{}
我设法得到这个错误的另一种方法是,通过定义对象的static std::vector
,其类型是在类内部定义的:
template<typename Ty>
class MyTemplatedClass
{
public:
struct AStructThatBelongsToMyTemplatedClass
{
uint32_t aU32ThatsPartOfTheStructThatBelongsToMyTemplatedClass;
};
static uint32_t u32;
static AStructThatBelongsToMyTemplatedClass aStructInstance;
static std::vector<uint32_t> vectOfu32;
static std::vector<AStructThatBelongsToMyTemplatedClass> vectOfStructInstances;
};
template<typename Ty> uint32_t MyTemplatedClass<Ty>::u32;
template<typename Ty> std::vector<uint32_t> MyTemplatedClass<Ty>::vectOfu32;
// This produces error: need 'typename' before 'MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass' because 'MyTemplatedClass<Ty>' is a dependent scope
//template<typename Ty> MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass MyTemplatedClass<Ty>::aStructInstance;
// ...but this works ok:
template<typename Ty> typename MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass MyTemplatedClass<Ty>::aStructInstance;
// This produces error: declaration of 'std::vector<MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances' outside of class is not definition
//template<typename Ty> std::vector<MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances;
// ...but this works ok (the only thing I added was 'typename'):
template<typename Ty> std::vector<typename MyTemplatedClass<Ty>::AStructThatBelongsToMyTemplatedClass> MyTemplatedClass<Ty>::vectOfStructInstances;
正如其他人已经提到的,这个错误很可能是因为语法错误。我有一个Declaration of SomeClass outside of class is not definition
错误。问题是for循环后缺少}
:
void A::a_function() {
for (int i = 0; i < 20; ++i) {
// loop code
// note the missing } here
// function code
}
SomeClass* A::getSomeClassInstance() {
// function code
}
相关文章:
- Centos7 g++ "to_string is not in a member of std"
- 以某种方式告诉编译器"Do not process line of code"
- 开放CV垫"Sizes of input arguments do not match"的问题
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- 如何修复此错误"the value of 'x1' is not usable in a constant expression"?static_assert
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- C++ 小错误:"... is not a static data member of ‘class ...'"
- 在 c++ 中获取"is not a static member of class"错误
- 尝试使用 SFLM 顶点时出错:"Vertex is not a member of sf"
- 解决方法:"out of line definition of 'graph' does not match any declaration in 'graph'"
- "local variables at the outermost scope of the function may not use the same name as any parameter"是
- C++错误:" 'class' does not name a type"和"invalid use of incomplete type ‘struct ...' "
- Visual Studio: "str() is not a member of std::ostringstream"
- VC++ 2010 "array is not a member of std"错误
- 摆脱编译器警告"warning: result of call is not used"
- 消除警告"construction of local static object is not thread-safe"
- 开发C++ "to_string is not a member of std"错误
- "is not a member of"和"no matching function for call to"错误
- SFML 2.1 和 Xcode 4.6.3 - "The specified SDK " 当前的 OS X " does not appear to have all of the necessar