预编译头文件中出现错误 C2512
error C2512 in precompiled header file?
我遇到了一个非常奇怪的问题。 当我尝试运行下面的程序时,有一条错误消息:"错误 C2512:'记录':没有合适的默认构造函数可用"。 当我双击它时,它会将我定向到一个名为"xmemory0"的预编译只读头文件。 他们是否希望我更改只读文件? 这是它引导我到的文件中的代码段:
void construct(_Ty *_Ptr)
{ // default construct object at _Ptr
::new ((void *)_Ptr) _Ty(); // directs me to this line
}
这是程序:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
const int NG = 4; // number of scores
struct Record
{
string name; // student name
int scores[NG];
double average;
// Calculate the average
// when the scores are known
Record(int s[], double a)
{
double sum = 0;
for(int count = 0; count != NG; count++)
{
scores[count] = s[count];
sum += scores[count];
}
average = a;
average = sum / NG;
}
};
int main()
{
// Names of the class
string names[] = {"Amy Adams", "Bob Barr", "Carla Carr",
"Dan Dobbs", "Elena Evans"};
// exam scores according to each student
int exams[][NG]= { {98, 87, 93, 88},
{78, 86, 82, 91},
{66, 71, 85, 94},
{72, 63, 77, 69},
{91, 83, 76, 60}};
vector<Record> records(5);
return 0;
}
您没有 Record 的默认构造函数,vector<Record> records(5)
需要一个。 众所周知,Visual Studio对其模板错误消息没有帮助。 如果转到"输出"选项卡而不是"错误"选项卡,则会看到一连串错误消息,其中第一个是您找到的消息,最后一个应指向您的vector
行。
如果单击错误消息,然后切换到"输出"选项卡,向下滚动冗长的错误消息,直到找到对自己的代码的引用,然后找出修复程序。
下面是完整的错误消息。 我的源代码中的第 44 行是您vector
行:
D:dev12VCINCLUDExmemory0(588) : error C2512: 'Record' : no appropriate default constructor available
D:dev12VCINCLUDExmemory0(587) : while compiling class template member function 'void std::allocator<_Ty>::c
nstruct(_Ty *)'
with
[
_Ty=Record
]
D:dev12VCINCLUDExmemory0(723) : see reference to function template instantiation 'void std::allocator<_Ty>:
construct(_Ty *)' being compiled
with
[
_Ty=Record
]
D:dev12VCINCLUDEtype_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' bein
compiled
with
[
_Ty=Record
]
D:dev12VCINCLUDEvector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being c
mpiled
with
[
_Alloc=std::allocator<Record>
]
z.cpp(44) : see reference to class template instantiation 'std::vector<Record,std::allocator<_Ty>>' being compi
ed
with
[
_Ty=Record
]
相关文章:
- 错误 C2512 视觉C++(并且不是循环依赖项)
- 错误 C2512:没有可用的适当默认构造函数:在构造函数中声明带有参数的对象!
- c++ 错误错误 C2512 没有合适的默认构造函数可用
- 错误 C2512 和 C3892 - 像游戏一样构建吃豆人
- 错误 C2512 'DerivedClass':没有可用的适当默认构造函数
- 错误C2512没有合适的默认构造函数,即使我有一个
- 错误 C2512:没有可用的适当默认构造函数(不是类)
- 预编译头文件中出现错误 C2512
- 结构,数组错误 C2512
- 在构造函数调用中收到错误 C2512
- 错误C2512没有合适的默认构造函数可用
- 没有显式构造函数的结构中出现错误C2512
- 错误C2512:没有合适的默认构造函数可用
- 错误C2512,但我有可用的默认构造函数
- 错误C2512:没有合适的默认构造函数可用
- 为什么我得到错误c2512:没有适当的默认构造函数可用
- 错误 C2512:'std::basic_ostream<_Elem,_Traits>':没有适当的默认构造函数可用于仅 Visual Studio
- c++错误c2512:没有可用的默认构造函数
- 错误 C2512:'Tile':没有可用的适当默认构造函数
- 错误C2512:没有合适的默认构造函数可用-如果在构造函数中初始化属性,为什么