标签: declaration

声明typedef类型

struct mystruct
{
    int   i;
    double f;
} ;

typedef mystruct myotherstruct;

//the other .cpp file
struct mystruct;  //OK,this is a correct forward declaration.
struct myotherstruct; // error C2371?in vc2k8?: 'myotherstruct' : redefinition; different basic types
Run Code Online (Sandbox Code Playgroud)

大家好.为什么我不能转发声明myotherstruct?

c++ typedef declaration

6
推荐指数
1
解决办法
1504
查看次数

为什么必须在C++类定义中声明方法?

如果只定义此方法,则会出现编译错误.

void classA::testMethod() {    
}
Run Code Online (Sandbox Code Playgroud)

所以必须先声明:

class classA {    
   void testMethod();
};
Run Code Online (Sandbox Code Playgroud)

为什么要宣布这些?

我知道对于常见的C方法方法,它不需要声明,但它们只能被定义:

void test() {
}
Run Code Online (Sandbox Code Playgroud)

c++ declaration

6
推荐指数
3
解决办法
465
查看次数

重新申报错误

我理解了声明和定义之间的区别当我遇到疑问时,我正在练习一些问题,下面的代码要求我列出代码片段中的错误.

f(int a,int b)
{
     int a;
     a=20;
     return a; 
}
Run Code Online (Sandbox Code Playgroud)

为什么这会给出重新声明错误a?它不应该给出多重定义a因为:

  • f(int a,int b)- 这里a定义对了吗?
  • 并在函数体中,int a再次定义?

那为什么不是多重定义错误呢?

c c++ declaration definition

6
推荐指数
1
解决办法
1万
查看次数

是否可以使用'using'来声明对3个整数类型别名的引用?

我得到了一个练习,我需要使用类型别名来"引用3个整数".虽然我发现成功使用typedef我无法通过using引入来复制它c++11.

代码:

typedef int (& int_ref)[3]; \\success

using int_ref2 = (int &) [3]; \\error

我应该只使用类似......

using int_ref2 = int [3];

int_ref2 & iruvar ...

c++ arrays reference declaration c++11

6
推荐指数
1
解决办法
177
查看次数

`f();`如何成为下面的声明?

DR 1990的摘录:

在一个例子中

void f() {
    f();  // #1
}
Run Code Online (Sandbox Code Playgroud)

#1处的语句不明确,可以解析为表达式或声明.

c++ declaration language-lawyer c++17

6
推荐指数
1
解决办法
197
查看次数

为什么我不能像这样对std :: array进行脱盐?

为什么我不能像这样对std :: array进行脱盐?

#include <array>

struct Point
{
    float x;
    float y;
};

int main()
{
   std::array<Point, 3> m_points { 
      { 1.0f, 1.0f }, 
      { 2.0f, 2.0f }, 
      { 3.0f, 3.0f }
   };
}
Run Code Online (Sandbox Code Playgroud)

这样做我得到错误:

错误:初始化程序太多了 std::array<Point, 3ul>

但它的工作原理如下:

std::array<Point, 3> m_points { 
   Point{ 1.0f, 1.0f }, 
   Point{ 2.0f, 2.0f }, 
   Point{ 3.0f, 3.0f } 
};
Run Code Online (Sandbox Code Playgroud)

相比之下,std::map可以使用以下两种方式初始化:

   std::map<int, int> m1 {std::pair<int, int>{1,2}, std::pair<int, int>{3,4}}; 
   std::map<int, int> m2 {{1,2}, {3,4}};
Run Code Online (Sandbox Code Playgroud)

c++ aggregate initialization declaration c++14

6
推荐指数
1
解决办法
292
查看次数

kotlin中的全局对象声明

如何像在java 中一样在kotlin中 全局声明对象TextView tv;.

或者在不同方法/函数中调用相同变量的任何方法.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val textView: TextView = findViewById(R.id.texfirst) as TextView 

    textView.setOnClickListener {
        Toast.makeText(applicationContext,"Welcome to Kotlin ! $abc "+textView.text, Toast.LENGTH_LONG).show()
    }

    myFunction(textView)
}

fun myFunction(mtextv : TextView) {
    Toast.makeText(applicationContext,"This is  new  $abc "+mtextv.text, Toast.LENGTH_LONG).show()
}
Run Code Online (Sandbox Code Playgroud)

看上面的代码我用参数分开了函数TextView.我想要TextView第二个功能的对象.我的问题是:是否可以在没有参数的情况下调用函数,并且我能够获取TextView对象myFunction().

在android工作室学习kotlin.希望问题很清楚.

declaration kotlin android-studio

6
推荐指数
1
解决办法
5714
查看次数

在枚举中重新声明变量

在C中,如果我们在内部重新声明变量enum,那么编译器会给出一个错误"'我'重新声明为不同类型的符号".好的.

#include <stdio.h>

int i = 10;

struct S 
{ 
    enum 
    {
        i = 20
    }e; 
};

int main()
{
    printf("%d\n", i);
}
Run Code Online (Sandbox Code Playgroud)

但是,在C++中,如果我们在枚举中重新声明变量,那么它的工作正常.

#include <iostream>
using namespace std;

int i = 10;

struct S 
{ 
    enum 
    {
        i = 20
    }e; 
};

int main()
{
    cout<<i<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么C++编译器没有给重新声明变量带来错误?

c c++ enums declaration language-lawyer

6
推荐指数
1
解决办法
257
查看次数

如何初始化用auto关键字声明的循环计数器?

这是我的代码:

#include <iostream>
#include <vector>

void cumulative_sum_with_decay(std::vector<double>& v)
{
    for (auto i = 2; i < v.size(); i++) {
        v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
    }
}

void printv(std::vector<double>& v)
{
    std::cout << "{";
    for (auto i = 0; i < v.size() - 1; i++) {
        std::cout << i << ", ";
    }
    std::cout << v[v.size() - 1] << "}\n";
}

int main()
{
    auto v = std::vector<double>{1, 2, …
Run Code Online (Sandbox Code Playgroud)

c++ initialization declaration auto c++11

6
推荐指数
2
解决办法
290
查看次数

必须声明函数的类型吗?

关于 Haskell 我不完全理解的一件事是声明函数及其类型:这是您必须做的事情还是只是为了清晰起见而应该做的事情?还是在某些情况下您需要这样做,而不是全部?

haskell declaration function function-declaration

6
推荐指数
1
解决办法
1163
查看次数