问题陈述是创建命名路由.它应该生成一个类似'/ zombies /:name'的路径,其中:name是一个参数,并指向ZombiesController中的索引操作.将路线命名为"墓地"
资源是资源
zombies
id name graveyard
1 Ash Glen Haven Memorial Cemetary
2 Bob Chapel Hill Cemetary
3 Jim My Fathers Basement
Run Code Online (Sandbox Code Playgroud)
我的解决方案是
TwitterForZombies::Application.routes.draw do
match ':name' => 'Zombies#index', :as => 'graveyard'
end
Run Code Online (Sandbox Code Playgroud)
我也试过了
TwitterForZombies::Application.routes.draw do
match ':name' => 'Zombie#index', :as => 'graveyard'
end
Run Code Online (Sandbox Code Playgroud)
我在两种情况下得到的错误是
Sorry, Try Again
Did not route to ZombiesController index action with :name parameter
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么??
这是我一直在研究的Rational类:
rational.h
#include<iostream>
using namespace std;
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
int numerator,denominator;
public:
// the various constructors
Rational();
Rational(int);
Rational(int,int);
//member functions
int get_numerator()const{return numerator;}
int get_denominator()const{return denominator;}
// overloaded operators
// relational operators
bool operator==(const Rational&)const;
bool operator<(const Rational&)const;
bool operator<=(const Rational&)const;
bool operator>(const Rational&)const;
bool operator>=(const Rational&)const;
//arithmetic operators
Rational operator+(const Rational&);
Rational operator-(const Rational&);
Rational operator*(const Rational&);
Rational operator/(const Rational&);
//output operator
friend ostream& operator<<(ostream&, const Rational&);
};
#endif //RATIONAL_H
Run Code Online (Sandbox Code Playgroud)
rational.cpp
#include "rational.h"
// …Run Code Online (Sandbox Code Playgroud) #include<iostream>
using namespace std;
class abc
{
int a;
};
class xyz : public virtual abc
{
int b;
};
int main()
{
abc obj;
xyz obj1;
cout<<endl<<sizeof(obj);
cout<<endl<<sizeof(obj1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
答案将取决于编译器,但当我看到这个结果时,我感到很惊讶
~/Documents/workspace/tmp ‹.rvm-› $ ./class_sizes
4
16
Run Code Online (Sandbox Code Playgroud)
如果我删除虚拟关键字,那么分配的大小分别为4和8,这正是我的预期.
为什么额外的空间被准确占用?我怀疑它是针对vptr表或其他类似但不确定的.
我有一个Map<Range<Double>, String>检查特定Double值(分数)映射到String(级别)的位置.最终用户希望能够动态地更改此映射,从长远来看,我们希望有一个基于Web的GUI控制权,但从短期来看,他们很高兴有一个文件进入S3和编辑每当需要改变时.我不想S3为每个请求点击并希望缓存它,因为它不会太频繁地更改(每周一次左右).我不想让代码更改并退回我的服务.
这是我想出的 -
public class Mapper() {
private LoadingCache<Score, String> scoreToLevelCache;
public Mapper() {
scoreToLevelCache = CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<Score, String>() {
public String load(Score score) {
Map<Range<Double>, String> scoreToLevelMap = readMappingFromS3(); //readMappingFromS3 omitted for brevity
for(Range<Double> key : scoreToLevelMap.keySet()) {
if(key.contains(score.getCount())) { return scoreToLevelMap.get(key); }
}
throw new IllegalArgumentException("The score couldn't be mapped to a level. Either the score passed in was incorrect or the …Run Code Online (Sandbox Code Playgroud) 我想基于索引删除向量的元素,比如所有偶数索引元素.我已经阅读了关于擦除删除习惯用法,但看不到如何应用它.这是我试过的:
vector<int> line;
line.reserve(10);
for(int i=0;i<10;++i)
{
line.push_back(i+1);
}
for(unsigned int i=0;i<line.size();++i)
{
//remove the even indexed elements
if(i%2 == 0)
{
remove(line.begin(),line.end(),line[i]);
}
}
line.erase( line.begin(),line.end() );
Run Code Online (Sandbox Code Playgroud)
这会擦除整个矢量.我希望只删除已删除算法标记的元素.
然后我尝试了这个
for(unsigned int i=0;i<line.size();++i)
{
//remove the even indexed elements
if(i%2 == 0)
{
line.erase( remove(line.begin(),line.end(),line[i]),line.end() );
}
}
Run Code Online (Sandbox Code Playgroud)
由于在移除时存在问题,这再次不起作用,索引似乎在迭代矢量时移位.应该采取什么样的正确方法来实现这一目标.
我正在为interviewstreet.com挑战编写一些代码我的代码给出了NumberFormatException
import java.io.*;
public class BlindPassenger
{
public static void main(String [] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int t,n;
//System.out.println(line);
t = Integer.parseInt(line);
for(int i=0;i<t;++i)
{
line = br.readLine();
n = Integer.parseInt(line); --n;
if(n == 0)
{
System.out.println("poor conductor");
}
else
{
char direction='l',seat_posn='l';
int row_no = 0, relative_seat_no = 0;
row_no = (int) Math.ceil(n/5.0);
relative_seat_no = n % 5;
if(row_no % 2 == 0)
{
//even row, need to reverse …Run Code Online (Sandbox Code Playgroud) 我正在使用git-bash在Windows 8 Release Preview上使用gcc-4.7.1.
$ g++ -v
Using built-in specs.
COLLECT_GCC=c:\Users\nikhil bhardwaj\mingw64\bin\g++.exe
COLLECT_LTO_WRAPPER=c:/users/nikhil bhardwaj/mingw64/bin/../libexec/gcc/x86_64-w
64-mingw32/4.7.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /home/drangon/work/mingw-w64-dgn/source/gcc/configure --host=x8
6_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-nls --enable-languages=c,
c++,objc,obj-c++ --with-gmp=/home/drangon/work/mingw-w64-dgn/build/for_target --
enable-twoprocess --disable-libstdcxx-pch --disable-win32-registry --prefix=/hom
e/drangon/work/mingw-w64-dgn/target --with-sysroot=/home/drangon/work/mingw-w64-
dgn/target
Thread model: win32
gcc version 4.7.1 20120524 (prerelease) (GCC)
Run Code Online (Sandbox Code Playgroud)
当我尝试编译一个小代码片段时,
using namespace std;
struct node
{
int data;
node *left, *right;
};
node *newNode(int data)
{
node *node = new (struct node);
node->data = data;
node->left = nullptr;
node->right = NULL;
return node;
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误, …
我正在尝试用Spring 3,JPA 2和Hibernate 3创建一个应用程序.当y持久化实体时我遇到了问题:没有任何反应!数据未插入数据库中,也不执行查询.但是,当我使用query.getResultList()之类的请求时,select正常工作.
所以我认为我的问题只出现在持续/更新和事务管理器上,但我对spring并不是很好.你能帮我吗 ?
这是我的配置文件:
我的applicationContext.xml
<jee:jndi-lookup id="soireeentreamis_DS" jndi-name="jdbc/soireeentreamis" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="soireeentreamis_DS" />
<property name="dataSources">
<map>
<entry key="soireeentreamisDS" value-ref="soireeentreamis_DS" />
</map>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="soireeentreamisPU" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<bean id="soireeentreamisTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven transaction-manager="soireeentreamisTransactionManager" /> …Run Code Online (Sandbox Code Playgroud) 当我尝试执行我的 aws lambda function
1) Error injecting constructor, java.lang.NullPointerException
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:66)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.class(ServicesResource.java:56)
while locating in.nikhilbhardwaj.path.route.resources.ServicesResource
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction.<init>(HelloWorldIntentAction.java:44)
while locating in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction
while locating in.nikhilbhardwaj.path.alexa.intent.IntentAction annotated with @com.google.inject.multibindings.Element(setName=,uniqueId=10, type=MAPBINDER, keyType=java.lang.String)
at in.nikhilbhardwaj.path.alexa.intent.IntentModule.configure(IntentModule.java:17) (via modules: in.nikhilbhardwaj.path.alexa.AlexaStarterApplicationModule -> in.nikhilbhardwaj.path.alexa.intent.IntentModule -> com.google.inject.multibindings.MapBinder$RealMapBinder)
while locating java.util.Map<java.lang.String, in.nikhilbhardwaj.path.alexa.intent.IntentAction>
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl.<init>(IntentHandlerServiceImpl.java:16)
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerService
for parameter 0 at in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet.<init>(AlexaStarterSpeechlet.java:26)
while locating in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.initializeServiceIds(ServicesResource.java:88)
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:68)
at in.nikhilbhardwaj.path.route.resources.ServicesResource$$FastClassByGuice$$3d6e91ec.newInstance(<generated>)
Run Code Online (Sandbox Code Playgroud)
我已经classpath按照 …
我们ApplicationConfig像这样定义了一些bean-
@Bean
S3Repository s3Repository() {
AmazonS3 s3 = new AmazonS3Client(s3AdminReadWriteCreds());
return new S3Repository(s3);
}
@Bean
S3Repository s3PrivateContentRepository() {
AmazonS3 s3 = new AmazonS3Client(readOnlyS3Creds());
return new S3Repository(s3);
}
@Bean
S3Repository s3SyncFilesContentRepository() {
AmazonS3 s3 = new AmazonS3Client(readOnlySpecificBucketCreds());
return new S3Repository(s3);
}
Run Code Online (Sandbox Code Playgroud)
这就是它们在代码中的用法-
public class AssetReader {
@Autowired
private S3Repository s3PrivateContentRepository;
.... used inside the class ....
}
Run Code Online (Sandbox Code Playgroud)
同样,其他bean的名称与预期产生它们的方法相同。
该应用程序运行良好,但是对此我感到有些惊讶,我不确定是否带有Admin凭据的bean是否会偶然地自动连接到任何地方,或者由于Spring的一些实现细节而是否连接了正确的bean?
我认为,如果自动装配可能产生歧义,则必须指定一个限定符。假设这能按预期进行,那么我们是否有任何理由使这些bean合格?