小编The*_*ter的帖子

如何让std :: make_unique成为我班级的朋友

我想声明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)

c++ templates unique-ptr friend-function c++14

27
推荐指数
1
解决办法
4684
查看次数

如何使用v7工具栏上的工具栏主页按钮提供向上导航

我的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 android-toolbar

16
推荐指数
3
解决办法
2万
查看次数

按菜单按钮时应用程序崩溃

我正在尝试为Android创建一个应用程序,我遇到了以下问题:

当我按下菜单按钮时,应用程序在特定手机中崩溃.我先告诉你一些细节.

  • 只有使用Android 4.1.2的LG Optimus L3 II e430才会出现此错误(到目前为止已在其他四款手机上测试过)
  • 应用程序以启动屏幕开始,没有操作栏.此时菜单按钮不起作用.
  • 通过简单的触摸,我们可以通过启动画面,然后转到实现ActionBar活动的主活动,并有一个导航抽屉.
  • 从这一点开始,每次我尝试单击菜单按钮时,应用程序崩溃.

这是菜单的布局和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生成的.

到目前为止,我尝试过:

  • 试图从sdk源(API级别16和21)查看存在问题的文件,但它们与堆栈跟踪无关(在没有意义的位置指向的堆栈跟踪中显示的行). …

crash android

9
推荐指数
1
解决办法
2328
查看次数

如何从友元函数访问受保护的构造函数?

我创建了一个类,我想强制任何试图构建对象的人使用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唯一方法.

编辑

我改变了一下代码.我没有提到我的类有一个模板参数.这显然使事情变得更加复杂.

c++ friend-function

8
推荐指数
1
解决办法
1110
查看次数

Android:为什么我们需要使用R2代替R和butterknife?

我已经使用奶油刀几个月了,我刚刚在其文档中注意到它说:

现在确保在所有Butter Knife注释中使用R2而不是R.

这是为什么?我一直在使用R,一切都很完美.

android butterknife

8
推荐指数
1
解决办法
4010
查看次数

意外的Haskell Aeson警告:'toJSON'没有明确的实现

我正在尝试使用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)

haskell aeson

5
推荐指数
1
解决办法
605
查看次数

克隆gitlab项目,使用git lfs而不是一直提供密码

我决定在gitlab上尝试git lfs.我注意到它不适用于ssh所以我决定使用https.Push工作正常,但当我尝试克隆我的项目时,它要求我输入每个文件的用户名和密码.

那有点烦人.它有什么解决方法吗?


编辑2018年

这个问题持续存在,真正的解决方案在哪里?它有简单直接的配方吗?

链接https://git-scm.com/docs/gitcredentialsgit-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 问题克隆过程不完整(错误),并一直在 给用户和密码 ...

尝试做凭证时也出现问题(参见 …

git gitlab git-lfs

5
推荐指数
1
解决办法
1162
查看次数

如何计算PBR中的镜面贡献?

我正在尝试在我们的项目中实现基于物理的渲染(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)

opengl glsl game-engine specular pbr

5
推荐指数
1
解决办法
2023
查看次数

是否可以在Android上增加浮动操作按钮大小?

我正在开发一个应用程序,我们希望在更大的设备上增加浮动操作按钮的大小.

问题是浮动动作按钮只有两种尺寸(mininormal).

首先,我试图设置一个自定义android:layout_heightandroid:layout_height我的工厂,但它没有工作.好吧,整个布局确实变得更大,但是在工厂周围出现了边框,背景并不完全透明.

迫切需要一个解决方案我创建了自己的圆形按钮视图并替换了浮动操作按钮.但这还不够好.我不会有很酷的涟漪效应或工厂的其他一切.

之后,我开始阅读Floating Action Button的源代码,但它变得非常复杂,所以我决定休息一下并在这里发布我的问题.

那么,有没有人知道如何增加浮动动作按钮的大小?

编辑20/7/2016

我的问题确实类似于这个问题.但是,我问如何增加Button的默认大小,而不是如何添加新大小.

android floating-action-button

3
推荐指数
1
解决办法
4207
查看次数

从子类调用基类

让我们说有这个:

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的平局()?

c++ superclass method-call

2
推荐指数
1
解决办法
231
查看次数