我想声明std::make_unique
函数是我班级的朋友.原因是我想声明我的构造函数protected
并提供一种使用创建对象的替代方法unique_ptr
.这是一个示例代码:
#include <memory>
template <typename T>
class A
{
public:
// Somehow I want to declare make_unique as a friend
friend std::unique_ptr<A<T>> std::make_unique<A<T>>();
static std::unique_ptr<A> CreateA(T x)
{
//return std::unique_ptr<A>(new A(x)); // works
return std::make_unique<A>(x); // doesn't work
}
protected:
A(T x) { (void)x; }
};
int main()
{
std::unique_ptr<A<int>> a = A<int>::CreateA(5);
(void)a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我收到此错误:
Start
In file included from prog.cc:1:
/usr/local/libcxx-head/include/c++/v1/memory:3152:32: error: calling a protected constructor of class 'A<int>'
return …
Run Code Online (Sandbox Code Playgroud) 我的activity(import android.support.v7.widget.Toolbar;
)中有一个工具栏,我正在尝试使用其主页按钮提供向上导航.是)我有的:
表现:
<!-- ... -->
<activity android:name=".SettingsActivity"
android:label="@string/settings"
android:parentActivityName=".MainActivity"/>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
view_toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
activity_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<include
layout="@layout/view_toolbar" />
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
我的onCreate方法:
super.onCreate(bundle)
setContentView(R.layout.activity_settings);
// Set the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Run Code Online (Sandbox Code Playgroud)
到目前为止,我不应该有一个按钮,我没有.所以我们没事.但是当我试图添加它时,我做不到.
首先我尝试了这个:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
没工作.然后,我想这(如图所示这里):
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ToolbarActivity.this, "Up clicked", …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Android创建一个应用程序,我遇到了以下问题:
当我按下菜单按钮时,应用程序在特定手机中崩溃.我先告诉你一些细节.
这是菜单的布局和onCreateOptionsMenu函数:
RES /菜单/ main.xml中
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
Run Code Online (Sandbox Code Playgroud)
部分来自MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
请注意,此代码是从Android Studio生成的.
到目前为止,我尝试过:
我创建了一个类,我想强制任何试图构建对象的人使用unique_ptr
.为此,我想到声明构造函数protected
并使用friend
返回a 的函数unique_ptr
.所以这是我想要做的一个例子:
template <typename T>
class A
{
public:
friend std::unique_ptr<A<T>> CreateA<T>(int myarg);
protected:
A(int myarg) {}
};
template <typename T>
std::unique_ptr<A<T>> CreateA(int myarg)
{
// Since I declared CreateA as a friend I thought I
// would be able to do that
return std::make_unique<A<T>>(myarg);
}
Run Code Online (Sandbox Code Playgroud)
我做了一些有关朋友函数的阅读,我理解朋友函数可以访问类对象的私有/受保护成员.
无论如何我可以让我的榜样有效吗?
即使没有朋友功能,我的目标也是让某人创建对象CreateA
的唯一方法.
编辑
我改变了一下代码.我没有提到我的类有一个模板参数.这显然使事情变得更加复杂.
我正在尝试使用aeson库进行json解析,我正在关注文档.这是我现在的代码:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import Data.Aeson as Ae
import Data.Text as T
import qualified Data.ByteString.Lazy as BS
import GHC.Generics
data Episode = Episode { season :: Int
, epNum :: Int
} deriving (Show, Generic)
data Series = Series { title :: !T.Text
, curEpisode :: Episode
} deriving (Show, Generic)
instance FromJSON Episode
instance ToJSON Episode -- Warning here
instance FromJSON Main.Series
instance ToJSON Main.Series -- Warning here
Run Code Online (Sandbox Code Playgroud)
问题是我得到了这两个警告:
src\Main.hs:21:10: Warning:
No …
Run Code Online (Sandbox Code Playgroud) 我决定在gitlab上尝试git lfs.我注意到它不适用于ssh所以我决定使用https.Push工作正常,但当我尝试克隆我的项目时,它要求我输入每个文件的用户名和密码.
那有点烦人.它有什么解决方法吗?
这个问题持续存在,真正的解决方案在哪里?它有简单直接的配方吗?
链接https://git-scm.com/docs/gitcredentials和git-lfs/wiki/Tutorial可能有些东西,但没有客观解决方案.
我描述的情况git lfs env
,
git-lfs/2.4.0 (GitHub; linux amd64; go 1.8.3)
git version 2.7.4
LocalWorkingDir=
LocalGitDir=
LocalGitStorageDir=
LocalMediaDir=lfs/objects
LocalReferenceDir=
TempDir=lfs/tmp
ConcurrentTransfers=3
TusTransfers=false
BasicTransfersOnly=false
SkipDownloadErrors=false
FetchRecentAlways=false
FetchRecentRefsDays=7
FetchRecentCommitsDays=0
FetchRecentRefsIncludeRemotes=true
PruneOffsetDays=3
PruneVerifyRemoteAlways=false
PruneRemoteName=origin
LfsStorageDir=lfs
AccessDownload=none
AccessUpload=none
DownloadTransfers=basic
UploadTransfers=basic
git config filter.lfs.process = "git-lfs filter-process"
git config filter.lfs.smudge = "git-lfs smudge -- %f"
git config filter.lfs.clean = "git-lfs clean -- %f"
Run Code Online (Sandbox Code Playgroud)
当我做git clone https://github.com/myPrivate/project1
问题克隆过程不完整(错误),并一直在 给用户和密码 ...
尝试做凭证时也出现问题(参见 …
我正在尝试在我们的项目中实现基于物理的渲染(PBR)(我们开始用于学术和学习目的的小型游戏引擎)并且我无法理解根据材料的金属和粗糙度计算镜面反射和漫反射贡献的正确方法是什么.
我们不使用任何第三方库/引擎进行渲染,一切都是用OpenGL 3.3手写的.
现在我有这个(我将把完整的代码放在下面):
// Calculate contribution based on metallicity
vec3 diffuseColor = baseColor - baseColor * metallic;
vec3 specularColor = mix(vec3(0.00), baseColor, metallic);
Run Code Online (Sandbox Code Playgroud)
但我的印象是,镜面术语必须以某种方式依赖于粗糙度.我想把它改成这个:
vec3 specularColor = mix(vec3(0.00), baseColor, roughness);
Run Code Online (Sandbox Code Playgroud)
但同样,我不确定.做正确的方法是什么?是否有正确的方法,或者我应该使用'试错'方法,直到我得到满意的结果?
这是完整的GLSL代码:
// Calculates specular intensity according to the Cook - Torrance model
float CalcCookTorSpec(vec3 normal, vec3 lightDir, vec3 viewDir, float roughness, float F0)
{
// Calculate intermediary values
vec3 halfVector = normalize(lightDir + viewDir);
float NdotL = max(dot(normal, lightDir), 0.0);
float NdotH = max(dot(normal, halfVector), 0.0);
float NdotV …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我们希望在更大的设备上增加浮动操作按钮的大小.
问题是浮动动作按钮只有两种尺寸(mini
和normal
).
首先,我试图设置一个自定义android:layout_height
和android:layout_height
我的工厂,但它没有工作.好吧,整个布局确实变得更大,但是在工厂周围出现了边框,背景并不完全透明.
迫切需要一个解决方案我创建了自己的圆形按钮视图并替换了浮动操作按钮.但这还不够好.我不会有很酷的涟漪效应或工厂的其他一切.
之后,我开始阅读Floating Action Button的源代码,但它变得非常复杂,所以我决定休息一下并在这里发布我的问题.
那么,有没有人知道如何增加浮动动作按钮的大小?
编辑20/7/2016
我的问题确实类似于这个问题.但是,我问如何增加Button的默认大小,而不是如何添加新大小.
让我们说有这个:
class A1
{
public:
void draw(){}
};
class A2
{
public:
void draw(){}
};
class A : public A1, public A2
{};
void main()
{
A a;
// I want to invoke the draw() of A1. How can I do that?
}
Run Code Online (Sandbox Code Playgroud)
如果我只是像a.draw()这样做,它就不会让我,因为A1 :: draw()和A2 :: draw()都与此匹配.在这种情况下我该怎么办?我如何调用A1的平局()?
android ×4
c++ ×3
aeson ×1
butterknife ×1
c++14 ×1
crash ×1
game-engine ×1
git ×1
git-lfs ×1
gitlab ×1
glsl ×1
haskell ×1
method-call ×1
opengl ×1
pbr ×1
specular ×1
superclass ×1
templates ×1
unique-ptr ×1