这可能很简单,但我无法弄清楚。
在我的 main.cpp 我有:
Image image("/path/to/image.png");
int* h = ImageProcessor::dailyPrices(image);
Run Code Online (Sandbox Code Playgroud)
在我的 ImageProcessor.h 中,我有:
//Maybe something is wrong with this? I am trying to forward declare.
namespace Magick
{
class Image;
}
class ImageProcessor {
public:
ImageProcessor();
virtual ~ImageProcessor();
static int* dailyPrices(const Magick::Image& abp_chart);
};
Run Code Online (Sandbox Code Playgroud)
在 ImageProcessor.cpp 我有
int* dailyPrices(const Image& abp_chart)
{
Run Code Online (Sandbox Code Playgroud)
但是在尝试编译时,我在 main.cpp 中收到以下错误:
path/to/VendBot.cpp:17: undefined reference to `ImageProcessor::dailyPrices(Magick::Image const&)'
Run Code Online (Sandbox Code Playgroud) 此 C++ 代码在编译时产生链接器错误:
// A.h
class A {
public:
static void f();
private:
static std::vector<int> v;
};
// A.cpp
void A::f() {
// this line is causing trouble
int i = v.size();
}
Run Code Online (Sandbox Code Playgroud)
将向量声明移动到 cpp 文件中是可行的。但是我想了解"Undefined symbols"上面代码中的链接器错误原因。是什么导致了上述代码中的链接器错误?
我知道这是一个很简单的问题,但没有人能给出简单的答案。
从 Form1 中的静态方法获取现有的 Form1 实例
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
callMethod();
}
public static void callMethod()
{
// how can get existing Form1 instance here ?
statusLabel.Text = "test";
}
}
Run Code Online (Sandbox Code Playgroud)
拜托,为什么我需要这个并不重要,重要的是知道如何。
所以基类中有一个静态方法,子类应该使用它。
但是,我需要知道哪个子类调用了静态方法。
代码是这样的:
class BaseClass():
@staticmethod
def getname():
#some magic
class SubClassA(BaseClass):
pass
class SubClassB(BaseClass):
pass
SubClassA.getname() #hope to see 'SubClassA'
SubClassB.getname() #hope to see 'SubClassB'
Run Code Online (Sandbox Code Playgroud)
或者,这甚至可能吗?
我目前正在构建一个 ASP.NET Web 应用程序。我想创建一些静态方法作为辅助方法。这是个好主意还是我以后会遇到问题?没有字段或属性。只是方法,有些有返回类型,有些没有返回类型。
静态方法是否在所有用户之间共享(例如字段和属性)还是唯一的?
private static string userName;
public static string UserName
{
get
{
if (User.Identity.IsAuthenticated)
{
if (userName == "" || userName == null)
{
userName = User.Identity.Name;
}
return userName;
}
else
{
throw new ArgumentNullException("Illegal Access", "You're not login or authorize to perform such task");
}
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个使用名为 GetUrl 的方法的部分视图,但出现错误cannot access static method in a non static context。
以下是我如何实现该方法:
public class TimeLineStep
{
public string Code { get; set; }
public string Title { get; set; }
public TimeLineStatus Status { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public static string GetUrl(string code)
{
switch (code)
{
case "1":
return "#";
case "2":
return "#";
case "3":
return "#";
case "4":
return "#";
case "5": …Run Code Online (Sandbox Code Playgroud) 我正在使用 laravel 5。在模型中,我有一个静态函数,我在控制器中调用它。它工作正常,但我希望这个函数与另一个非静态函数进行相同的更改,当我在静态函数内调用它时,它会产生错误。
Non-static method App\Models\Course::_check_existing_course() should not be called statically
Run Code Online (Sandbox Code Playgroud)
这是我的模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Course extends Model {
public $course_list;
protected $primaryKey = "id";
public function questions(){
return $this->belongsToMany('App\Models\Question','course_questions')->where("status",1)->orderBy("id","DESC");
}
public static function courses_list(){
self::_check_existing_course();
}
private function _check_existing_course(){
if(empty($this->course_list)){
$this->course_list = self::where("status",1)->orderBy("course")->get();
}
return $this->course_list;
}
}
Run Code Online (Sandbox Code Playgroud) 我必须@staticmethod在类内部创建一个。我想知道是否有任何方法可以“保存”在两个顺序调用之间的静态方法内部定义的变量。
我的意思是一个行为类似于 C++ 中的静态变量的变量
我在 serializers.py 中使用了 PyCharm 编写了一些方法。然后我必须编写一个方法来获取名称。
def get_artist_name(obj):
return obj.artist.name
Run Code Online (Sandbox Code Playgroud)
然后 PyCharm 建议我将方法设为静态。
@staticmethod
def get_artist_name(obj):
return obj.artist.name
Run Code Online (Sandbox Code Playgroud)
从那以后我想知道它有什么好处?这是一个很好的做法或类似的东西?如果有我可以阅读有关此特定主题的任何文档,请提前致谢。
想象一下这样的事情......
public class Result
{
public string Name {get; set;}
public int Score {get; set;}
public bool Pass {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
还有一个静态方法...
public static Result SetPass(this Result result)
{
result.Pass = result.Score > 50;
return result;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我必须返回结果还是已经修改到位?我可以让返回类型为空,然后遍历结果集合并像这样修改......
foreach (var result in results)
{
result.SetPass();
}
Run Code Online (Sandbox Code Playgroud)
还是我需要返回结果对象并重新分配?