在Kotlin中,是否可以使用工厂函数创建具有私有构造函数的类的实例?
我的目标是强制使用工厂函数并通过类的构造函数防止实例化.
例:
// factory function, valid
val myInstance = myClassOf()
// class instantiation, invalid
val myInstance = MyClass()
Run Code Online (Sandbox Code Playgroud)
我试图模仿一些内置工厂函数的行为intArrayOf(),例如
// works
val myIntArray = intArrayOf()
// not possible as IntArray has a private constructor
val myIntArray = IntArray()
Run Code Online (Sandbox Code Playgroud) 嘿伙计们,我是C的新手,对于我的第一个项目,我需要实现一个基于数组的队列.我希望我的队列能够保存任何类型的对象,因此我创建了一个QueueElement结构来保存指向任何类型对象的void指针.我认为一切正常,除了我无法从QueueElement结构中读取'position'和'value'字段.我尝试编译时遇到以下错误.
错误:
Runnable.c: In function `main':
Runnable.c:10: error: dereferencing pointer to incomplete type
Runnable.c:11: error: dereferencing pointer to incomplete type
Run Code Online (Sandbox Code Playgroud)
我很确定我只是没有正确投射.任何帮助表示赞赏.
再次感谢,Pooch
Runnable.c
#include <stdio.h>
#include "Queue.h"
int main(void) {
int i = 9;
Queue q = CreateQueue();
QueueElement e = CreateQueueElement(&i);
Enqueue(q, e);
QueueElement f = Dequeue(q);
/* PROBLEM IS HERE */
printf("position: %d", f->position);
printf("value: %d", (int *)(f->value));
DestroyQueue(q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include "QueueElement.h"
typedef struct QueueStruct *Queue;
Queue CreateQueue(void);
void DestroyQueue(Queue q); …Run Code Online (Sandbox Code Playgroud) 嘿伙计们,我正在研究我的第一个C++课程.出于某种原因,我在尝试编译时遇到以下错误:
`truncate' undeclared (first use this function)
Run Code Online (Sandbox Code Playgroud)
完整来源:
#include <iostream>
#include <math.h>
using namespace std;
#define CENTIMETERS_IN_INCH 2.54
#define POUNDS_IN_KILOGRAM 2.2
int main() {
double feet, inches, centimeters, weight_in_kg, weight_in_lbs;
// get height in feet and inches
cout << "Enter height (feet): ";
cin >> feet;
cout << "Enter (inches): ";
cin >> inches;
// convert feet and inches into centimeters
centimeters = ((12 * feet) + inches) * CENTIMETERS_IN_INCH;
// round 2 decimal places and truncate
centimeters = truncate(centimeters); …Run Code Online (Sandbox Code Playgroud) 我是C++的新手,需要第一次使用库.我希望有人能够告诉我如何正确地[链接到/包含]图书馆.
我想使用的库是ID3 v3.8.8,可以在这里找到:http://id3lib.sourceforge.net/
我已经下载了Windows二进制文件,现在只需要一种链接到库的方法.
下载的文件:Debug/id3lib.dll,Debug/id3lib.lib,Debug/id3lib.exp,Release/id3lib.dll,Release/id3lib.lib,Release/id3lib.exp
我正在使用Visual Studio 2010.
任何帮助是极大的赞赏.提前致谢.
我试图动态地分配内存到堆,然后删除分配的内存.下面的代码让我很难过:
// String.cpp
#include "String.h"
String::String() {}
String::String(char* source)
{
this->Size = this->GetSize(source);
this->CharArray = new char[this->Size + 1];
int i = 0;
for (; i < this->Size; i++) this->CharArray[i] = source[i];
this->CharArray[i] = '\0';
}
int String::GetSize(const char * source)
{
int i = 0;
for (; source[i] != '\0'; i++);
return i;
}
String::~String()
{
delete[] this->CharArray;
}
Run Code Online (Sandbox Code Playgroud)
以下是编译器尝试删除CharArray时出现的错误:
0xC0000005:访问冲突读取位置0xccccccc0.
这是堆栈上的最后一次调用:
msvcr100d.dll!operator delete(void*pUserData)第52行+ 0x3字节C++
我相当肯定这段代码中存在错误,但会为您提供所需的任何其他信息.哦,是的,使用VS 2010 for XP.
编辑:继承我的String.h
// String.h - string class
#pragma once
#define NOT_FOUND …Run Code Online (Sandbox Code Playgroud)