我有两个类,障碍和boid,boid继承自障碍.
现在我想编写一些可以处理这两个类的对象的函数,这样传递vector<boid>就可以了vector<obstacle>.
当我像这样进行类型转换并尝试访问向量的大小时,我得到的数字是1840700394而不是60:
vector<boid>* boids; ....
cout << ((vector<obstacle>*)boids)->size() << endl;
Run Code Online (Sandbox Code Playgroud)
我也试过"reinterpret_cast"但同样的问题.
我正在编写一段代码,其中包含如下声明:
if (memcmp (version, "\x0\x0\x0", 3) == 0 )
Run Code Online (Sandbox Code Playgroud)
与版本进行比较的数据是什么?是否与"000"相比?
可以告诉我这个代码有什么不对吗?它编译和一切都很好,但输出是固定的零一直下降.所以它不算数字.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const char FileName[] = "c:/test.txt";
int main ()
{
string lineBuffer;
ifstream inMyStream (FileName); //open my file stream
if (inMyStream.is_open())
{
//create an array to hold the letter counts
int upperCaseCount[26] = {0};
int lowerCaseCount[26] = {0};
//read the text file
while (!inMyStream.eof() )
{
//get a line of text
getline (inMyStream, lineBuffer);
//read through each letter in the lineBuffer
char oneLetter;
for( int n=0; n < (int)lineBuffer.length(); ++n ) …Run Code Online (Sandbox Code Playgroud) 我想专注于以下模板:
template <typename T, int P>
T item(size_t s);
Run Code Online (Sandbox Code Playgroud)
这样的事情:
template<int P>
T item<typename T>(size_t s)
{
//for all numeric types
return static_cast<T>(rand());
}
template <int P>
string item<string>(size_t s)
{
return "asdf";
}
Run Code Online (Sandbox Code Playgroud)