小编K-b*_*llo的帖子

c ++中的静态地图初始化

有许多类似的问题,实际上我从其他几个帖子中编写了以下代码.不幸的是,我仍然有一个错误,我似乎无法破解 - 虽然我做了很多15年前的c ++开发.

我想使用地图制作一个简单的静态查找表.

这是到目前为止的代码(代码css似乎不能很好地呈现它):

enum RegionCodeEnum
{
    One,
    Two,
    Three
};

enum DeviceCodeEnum
{
    AAA,
    BBB,
    CCC
};

class LookupTable
{
    friend class constructor;

    struct constructor 
    {
        constructor() 
        { 
            table[One] = AAA;
            table[Two] = AAA;
            table[Three] = CCC;
        }
    };

    static constructor cons;

public:
    LookupTable(void);

    static DeviceCodeEnum GetDeviceFromRegion(RegionCodeEnum RegionCode);

private:
    static map<RegionCodeEnum, DeviceCodeEnum> table;
};

LookupTable::constructor LookupTable::cons;

LookupTable::LookupTable(void)
{

}

DeviceCodeEnum LookupTable::GetDeviceFromRegion(RegionCodeEnum RegionCode)
{
    return table[RegionCode];
}
Run Code Online (Sandbox Code Playgroud)

从其他地方代码我有这个代码:

DeviceCodeEnum code= LookupTable::GetDeviceFromRegion(One);
Run Code Online (Sandbox Code Playgroud)

我得到的编译错误是:

error LNK2001: unresolved external symbol "private: …
Run Code Online (Sandbox Code Playgroud)

c++ static initialization map

0
推荐指数
1
解决办法
1381
查看次数

在选项卡中显示任何内容时出现NullPointerException

我试图在同一个活动中制作一个标签界面.这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
    />
    <FrameLayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <AnalogClock android:id="@+id/tab1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      />
      <Button android:id="@+id/tab2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="A semi-random button"
      />
    </FrameLayout>
  </LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)

dummy.xml:

<?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="vertical" >

      <RatingBar
          android:id="@+id/ratingBar1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

  </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

和我的活动:

public class LActivity extends Activity {


 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);


     TabHost tabs=(TabHost)findViewById(R.id.tabhost); …
Run Code Online (Sandbox Code Playgroud)

android android-tabs

0
推荐指数
1
解决办法
272
查看次数

查询java中的变量范围

public class Knowing {

    static final long tooth = 343L;

    static long doIT(long tooth) {
        System.out.print(++tooth + " ");
        return ++tooth;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.print(tooth + " ");
        final long tooth = 340L;

        new Knowing().doIT(tooth);
        System.out.println(tooth);
    }
}
Run Code Online (Sandbox Code Playgroud)

好的,这是我的问题:

  • 如果我们声明了一个全局变量static final long tooth = 343L;我们怎么能在声明的main方法中有另一个变量final long tooth = 340L;我只想知道为什么这是允许的,因为我运行它并且没有错误?

  • 并且也不应该通过使用className.variableName来访问全局静态变量齿,而不是通过创建一个新的instance.variable名称,只有一个警告才允许这样做?

java

0
推荐指数
1
解决办法
149
查看次数

这个指针在构造函数中

下面的代码是类的构造函数,这个类有一个成员

int               ** busyhours      ;
Run Code Online (Sandbox Code Playgroud)

构造函数

Instructor::Instructor ( int id , string name )
{
  this->id   = id   ;
  this->name = name ;

  // initialize busyhours
  this->busyhours = new int * [DAYS_WORKING] ;
  for ( int i = 0 ; i < DAYS_WORKING ; i++ )
  {
       busyhours[i] = new int[HOURS_PER_DAY] ;
       for ( int j = 0 ; j < HOURS_PER_DAY ; j++ )
        busyhours[i][j] = 0 ;
  }
}
Run Code Online (Sandbox Code Playgroud)

busyhour成员首先与此指针一起使用但是在没有此指针的情况下使用它.我不明白为什么.谢谢你的回答.

c++ class this

0
推荐指数
1
解决办法
199
查看次数

标签 统计

c++ ×2

android ×1

android-tabs ×1

class ×1

initialization ×1

java ×1

map ×1

static ×1

this ×1