草庐IT

c++ - 获取错误表达式必须具有 C++ 中的类类型

coder 2024-02-14 原文

我收到以下 IntelliSense 错误:

Expression must have class type f:\C++\prj\map1\map1\testMap1.cpp 11

这是指我的代码中的以下行(完整显示如下):

theMap.insert(1, "one");

我无法弄清楚问题是什么。它似乎与 theMap 的声明无关,但每次我尝试调用 theMap 上的方法时,我都会收到错误消息。这是我的代码:

map 1.h

#ifndef MAP_H
    #define MAP_H
    #include <list>
    #include <utility>
    using namespace std;

//pair class definition
template<typename F, typename S>
class Pair
{
public:
Pair(const F& a, const S& b);
F get_first() const;
S get_second() const;
private:
F first;
S second;
};

template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b):first(a),second(b){}

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
    return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
    return second;
}

template<typename K, typename V>
class map
{
public:
    map();
    void insert(const K& key, const V& value);
    bool contain_key(const K& key);
    V value_of(const K& key);
    void remove_key(const K& key);
    void print();

private:
    list<Pair<K, V>> theList;
};

template<typename K, typename V>
inline map<K, V>::map():{}

template<typename K, typename V>
inline void map<K, V>::insert(const K& key, const V& value)
{
    bool notThere = true;
    if(contain_key(key))
    {
        notThere = false;
    }
    if(notThere)
    {
    theList.push_back<pair<key, value>>
    }
}

template<typename K, typename V>
inline bool map<K, V>::contain_key(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0 : x< theList.size() ; x++)
    {
        temp = iter->first;
        if(temp == key)
        {
            return true;
        }
        iter.advance();
    }

    return false;
}

template<typename K, typename V>
inline V map<K, V>::value_of(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
            if(temp == key)
            {
                return iter->second;
            }
    }
    cout << “we don’t have this key " << key << " in the map” "\n";
    return 0;
}

template<typename K, typename V>
inline void map<K, V>::remove_key(const K& key)
{
    iterator iter = theList.begin();
    K temp;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
        if(temp == key)
        {
            theList.erase(iter)
        }

    }
}

template<typename K, typename V>
inline void map<K, V>::print()
{
    iterator iter = theList.begin;
    K temp;
    V temp2;
    for(int x=0; x < theList.size() ; x++)
    {
        temp = iter->first;
        temp2 = iter->second;
        cout << "Key:" << temp << " Value:" << temp2 << "\n";
    }



}

#endif;

testMap1.cpp

#include "map1.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
    map<int, string> theMap();
    theMap.insert(1, "one");
    theMap.insert(2, "two");
    theMap.insert(2, "double");
    theMap.insert(3, "three");

    theMap.print();

    theMap.remove_key(3);

    cout << "please enter a number" << "\n";
    int temp;
    cin >> temp;

    bool contains = theMap.contain_key(temp);
    if(contains)
    {
        cout << theMap.value_of(temp);
    }
    else
    {
        cout << "we don’t have this key " << temp << " in the map" << "\n";
    }


    return 0;
}

最佳答案

map<int, string> theMap();

这是声明一个函数 theMap 不调用 map 的默认构造函数

删除 ()

map<int, string> theMap/*()*/;

关于c++ - 获取错误表达式必须具有 C++ 中的类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072663/

有关c++ - 获取错误表达式必须具有 C++ 中的类类型的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  6. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  7. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  10. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

随机推荐