Why I can't do this? compiler says
为什么我不能这么做?编译器说
warning C4832: token '.' is illegal after UDT 'A : see declaration of 'A' error C2275: 'A' : illegal use of this type as an expression : see declaration of 'A'
警告C4832:令牌”。在UDT 'A:看到'A'错误C2275: 'A':非法使用这种类型作为表达式:参见'A'声明
class A{
public:
std::string getString(){return "olalal";}
};
class B{
public:
std::string getStringFromA() {return A.getString();}
};
int main()
{
B b;
cout << b.getStringFromA();
return 0;
}
3 个解决方案
#1
6
You can only call non-static
member functions on an object.
您只能在对象上调用非静态成员函数。
E.g.
如。
std::string B::getStringFromA()
{
A object_of_type_A;
return object_of_type_A.getString();
}
Alternatively, you can create a unnamed temporary A
and call getString()
on that, E.g.
或者,您可以创建一个未命名的临时a,并在其上调用getString()。
class B {
public:
std::string getStringFromA() { return A().getString(); }
};
If getString()
were declared static
in A
, you could call the function without an object, but the syntax uses ::
, not .
.
如果getString()在A中被声明为静态的,那么您可以在没有对象的情况下调用该函数,但是语法使用的是::not ..
E.g.
如。
std::string B::getStringFromA()
{
return A::getString();
}
With a function name like getStringFromA
, though, I would expect to pass a parameter of the particular A
that I wanted to get the string from, perhaps passing the A
by reference.
不过,对于一个函数名,比如getStringFromA,我希望通过一个特定a的参数来获得字符串,或者通过引用传递a。
class B {
public:
std::string getStringFromA(A& a) { return a.getString(); }
};
If getString
in A
doesn't actually modify the A
object it would be better to declare the function const
so that it can be called on const
instances of A
. In this case, a const
reference could be used.
如果A中的getString实际上并没有修改一个对象,那么最好声明函数const,这样就可以调用A的const实例。在这种情况下,可以使用const引用。
class A {
public:
std::string getString() const { return "olalal"; }
};
class B {
public:
std::string getStringFromA(const A& a) { return a.getString(); }
};
#2
4
getString()
is not a static member function; you must call it on a specific instance of A
, e.g. A a; a.getString();
.
getString()不是静态成员函数;你必须在a的特定实例上调用它,例如a;a.getString();。
#3
2
Since A::getString()
isn't a static method you need to call it on an instance of A
.
因为::getString()不是一个静态方法,您需要在一个实例上调用它。
#1
6
You can only call non-static
member functions on an object.
您只能在对象上调用非静态成员函数。
E.g.
如。
std::string B::getStringFromA()
{
A object_of_type_A;
return object_of_type_A.getString();
}
Alternatively, you can create a unnamed temporary A
and call getString()
on that, E.g.
或者,您可以创建一个未命名的临时a,并在其上调用getString()。
class B {
public:
std::string getStringFromA() { return A().getString(); }
};
If getString()
were declared static
in A
, you could call the function without an object, but the syntax uses ::
, not .
.
如果getString()在A中被声明为静态的,那么您可以在没有对象的情况下调用该函数,但是语法使用的是::not ..
E.g.
如。
std::string B::getStringFromA()
{
return A::getString();
}
With a function name like getStringFromA
, though, I would expect to pass a parameter of the particular A
that I wanted to get the string from, perhaps passing the A
by reference.
不过,对于一个函数名,比如getStringFromA,我希望通过一个特定a的参数来获得字符串,或者通过引用传递a。
class B {
public:
std::string getStringFromA(A& a) { return a.getString(); }
};
If getString
in A
doesn't actually modify the A
object it would be better to declare the function const
so that it can be called on const
instances of A
. In this case, a const
reference could be used.
如果A中的getString实际上并没有修改一个对象,那么最好声明函数const,这样就可以调用A的const实例。在这种情况下,可以使用const引用。
class A {
public:
std::string getString() const { return "olalal"; }
};
class B {
public:
std::string getStringFromA(const A& a) { return a.getString(); }
};
#2
4
getString()
is not a static member function; you must call it on a specific instance of A
, e.g. A a; a.getString();
.
getString()不是静态成员函数;你必须在a的特定实例上调用它,例如a;a.getString();。
#3
2
Since A::getString()
isn't a static method you need to call it on an instance of A
.
因为::getString()不是一个静态方法,您需要在一个实例上调用它。