在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
最满意答案
您的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. ...
-
Django会话范围(Django session scope)[2023-01-22]
您根本没有显示任何代码,这本来是有帮助的。 但我希望问题是你没有将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 ... -
Django会话管理(Django Session Management)[2021-10-06]
...但我无法在主页和主页功能上完美地重定向它 你的问题与会话无关。 基本上你需要, 成功login尝试后,将用户重定向到home page 在logout尝试后将用户重定向到login page 我建议你使用内置的身份验证视图 : Django提供了几个可用于处理登录,注销和密码管理的视图。 这些使用股票身份验证表格,但您也可以传递自己的表格。 使用这些内置身份验证视图,您还可以在settings.py文件中成功登录尝试后设置默认重定向URL: LOGIN_REDIRECT_URL = '/home/' ... -
Django - 失去认证会话(Django - Losing Auth Session)[2023-09-09]
这里只是一个猜测:您是否在您无法访问用户的视图中包含RequestContext? 换句话说,如果您调用泛型视图,则会自动包含RequestContext,但如果您使用的是render_to_response()则需要像这样调用它: return render_to_response('template_name', { your context dict }, context_instance=RequestContext(request)) Just a guess here: are ... -
Django会话last_touch(Django session last_touch)[2022-08-14]
您的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删除该行。 但是如果用户没有注销,那么该行永远不会被 ...
-
django缓存会话(django cached session)[2022-01-04]
重新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 ...
-
增加Django会话的时间(Increase time of Django Sessions)[2022-02-02]
它由名为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 ...