首页 \ 问答 \ 在c ++对象上使用extern时的未定义引用,但不是整数类型(Undefined reference when using extern on a c++ object, but not integral type)

在c ++对象上使用extern时的未定义引用,但不是整数类型(Undefined reference when using extern on a c++ object, but not integral type)

尝试在c ++对象上使用extern时,我得到未定义的引用错误。 整体类型似乎不会发生这种情况。 我错过了什么?! 下面的代码复制了这个问题:

file1.cpp:

#include <string>

const std::string s("test");
int i = 99;

int main()
{
        extern void Test();
        Test();
}

file2.cpp:

#include <iostream>
#include <string>

extern const std::string s;
extern int i;

void Test()
{
        std::cout << s << std::endl;
        std::cout << i << std::endl;
}

如果我注释掉's'std :: string变量的用法,那么链接错误就会消失。

关于SO的其他问题与此类似,但它们似乎都与未定义变量的人有关,我就是这样!


I'm getting undefined reference errors when trying to use extern on a c++ object. It doesn't appear to happen with integral types. What am I missing?! This code below replicates the problem:

file1.cpp:

#include <string>

const std::string s("test");
int i = 99;

int main()
{
        extern void Test();
        Test();
}

file2.cpp:

#include <iostream>
#include <string>

extern const std::string s;
extern int i;

void Test()
{
        std::cout << s << std::endl;
        std::cout << i << std::endl;
}

if i comment out the usage of the 's' std::string variable, the linking errors go away.

There are other questions on SO similar to this, but they all seem to be related to people not defining the variable, which I am!


原文:https://stackoverflow.com/questions/12701130
更新时间:2024-03-27 11:03

最满意答案

您的try / except块没有正确布局以处理KeyError 。 它可能发生在三个地方,但你只能抓住一个。 逻辑有点疯狂,但只是编辑你有什么,我认为这是你试图达到的目的:

try:
     if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
          logout(request)
          del request.session['last_touch']
          return self.get_response(request) 
     else:
          request.session['last_touch'] = datetime.now()
          return self.get_response(request)  
except KeyError: #KeyError thrown if last touch doesn't exist, so set it. 
     request.session['last_touch'] = datetime.now()

Your try/except block isn't laid out correctly to catch and deal with the KeyError. It can occur in three places, but you're only catching one. The logic is a bit janky, but just editing what you have, I think this is what you're trying to achive:

try:
     if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
          logout(request)
          del request.session['last_touch']
          return self.get_response(request) 
     else:
          request.session['last_touch'] = datetime.now()
          return self.get_response(request)  
except KeyError: #KeyError thrown if last touch doesn't exist, so set it. 
     request.session['last_touch'] = datetime.now()

相关问答

更多
  • 修改django_session表以添加显式user_id可以使生活更容易。 假设你这样做(或类似的东西),这里有四种方式来使事情变得你喜欢: 叉django.contrib.session代码。 我知道,这是一个可怕的建议。 但是它只有500行,包括所有后端,减去测试。 黑客很简单 这是最好的路线,只有当你要做一些严格的重新排列的事情。 如果你不想分叉,你可以尝试连接到Session.post_save信号和munge那里。 或者你可以MonkeyPatch contrib.session.models. ...
  • 您根本没有显示任何代码,这本来是有帮助的。 但我希望问题是你没有将request对象传递给“home”视图中的模板上下文:通常这会在你使用RequestContext或render快捷方式时自动发生,这可能是你在其他视图中做的。 You haven't shown any code at all, which would have been helpful. But I expect the problem is that you're not passing the request object to t ...
  • ...但我无法在主页和主页功能上完美地重定向它 你的问题与会话无关。 基本上你需要, 成功login尝试后,将用户重定向到home page 在logout尝试后将用户重定向到login page 我建议你使用内置的身份验证视图 : Django提供了几个可用于处理登录,注销和密码管理的视图。 这些使用股票身份验证表格,但您也可以传递自己的表格。 使用这些内置身份验证视图,您还可以在settings.py文件中成功登录尝试后设置默认重定向URL: LOGIN_REDIRECT_URL = '/home/' ...
  • 这里只是一个猜测:您是否在您无法访问用户的视图中包含RequestContext? 换句话说,如果您调用泛型视图,则会自动包含RequestContext,但如果您使用的是render_to_response()则需要像这样调用它: return render_to_response('template_name', { your context dict }, context_instance=RequestContext(request)) Just a guess here: are ...
  • 您的try / except块没有正确布局以处理KeyError 。 它可能发生在三个地方,但你只能抓住一个。 逻辑有点疯狂,但只是编辑你有什么,我认为这是你试图达到的目的: try: if datetime.now() - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0): logout(request) del request.session ...
  • 您需要在设置中添加SESSION_COOKIE_DOMAIN参数 SESSION_COOKIE_DOMAIN = '.example.com' 用于会话cookie的域。 将此设置为字符串,例如“.lawrence.com”(请注意前导点!)以用于跨域Cookie,或对于标准域Cookie使用None。 You need to add SESSION_COOKIE_DOMAIN parameter to your settings SESSION_COOKIE_DOMAIN = '.example.com ...
  • Django 文档说明(强调我): 清除会话存储 当用户在您的网站上创建新会话时,会话数据可能会累积在您的会话存储中。 如果您正在使用数据库后端,则django_session数据库表将增长。 如果您正在使用文件后端,则您的临时目录将包含越来越多的文件。 要了解此问题,请考虑数据库后端会发生什么。 当用户登录时,Django会在django_session数据库表中添加一行。 每次会话数据更改时,Django都会更新此行。 如果用户手动注销,则Django删除该行。 但是如果用户没有注销,那么该行永远不会被 ...
  • 重新1。 从我的经验来看差异很大。 在您的方案中,每次创建或更改会话时 - 这将生成数据库写入。 每次用户访问站点时,都会生成数据库读取。 将其移出数据库 - 每个请求至少保存一个查询。 此额外流量加上您的正常应用流量可能会轻易破坏您的数据库性能。 回覆。 2。 即使对于db支持的会话,也没有明确的方法来确保用户被记录一次(读取所有会话数据,去除它然后过滤不是一个明确的方式:)) 要做到这一点,我可能会使用缓存:每次用户登录时,检查缓存中是否有密钥(例如用户:),存储的值将是会话ID,如果 ...
  • 我遇到了同样的错误。 看起来,如果你来自快速cookie会话,可以设置req.session = {/* some arbitrary session object */} 。 显然, req.session在表示需求的实例上有一些方法。 因此,请确保您没有在代码中的任何位置明确覆盖req.session 。 I was having the same error. It seems that if you're coming from express cookie sessions, it was pos ...
  • 它由名为SESSION_COOKIE_AGE的设置驱动 文档在这里 如果要将其设置为18天,请在设置文件中 SESSION_COOKIE_AGE = 18 * 24 * 60 * 60 It is driven by a setting called SESSION_COOKIE_AGE Documentation here If you want to set it to 18 days , in your settings file, SESSION_COOKIE_AGE = 18 * 24 * 60 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)