标签: unassigned-variable

如何为具有引用成员变量的类实现operator =()?

我想要一个包含引用的对象,并将该对象放入向量中...

我必须在我想要推送到矢量的任何对象中使用智能指针而不是成员引用吗?这就是我想要做的:

#include <string>
#include <vector>
using namespace std;

class MyClass {
   public:
    MyClass(const string& str_ref);    //constructor
        MyClass(const MyClass& mc);        //copy constructor
   private:
        string& my_str;
};

MyClass::MyClass(const string& str_ref) :
    my_str(str_ref)
{}

MyClass::MyClass(const MyClass& mc) :
    my_str(mc.my_str)
{}


int main() {

    //create obj and pass in reference
    string s = "hello";
    MyClass my_cls(s);

    //put into vector
    vector<MyClass> vec;
    vec.push_back(my_cls);

    return 0;
}

//Throws Error
//ref.cpp:6:7: error: non-static reference member ‘std::string& MyClass::my_str’, can’t use default assignment operator
Run Code Online (Sandbox Code Playgroud)

但是它说我需要实现我自己的operator =(),因为默认生成的一个是无效的,但当然,没有合法的方法可以这样做......

#include <string> …
Run Code Online (Sandbox Code Playgroud)

c++ oop operator-overloading unassigned-variable

3
推荐指数
2
解决办法
640
查看次数

使用未分配的局部变量 - if语句

我正在执行以下代码块,并且编译器抱怨未分配的局部变量,并且可以使用一些帮助来识别出什么.

while (rsData.Read())
{
    if (rsData["TYPE"] != DBNull.Value)
        strType = rsData["TYPE"].ToString().Trim();


    if (strType == "01")
    {
        if (rsData["Text"] != DBNull.Value)
            strwho = rsData["Text"].ToString();

        if ((strwho.Length < 10 || (strwho.IndexOf("NULL") > 1)))
            strwho = "";
    }
    else if (strType == "07")
    {
        if (rsData["Text"] != DBNull.Value)
            strmetades = rsData["Text"].ToString();

        if ((strmetades.Length < 10 || (strmetades.IndexOf("NULL") > 1)))
            strmetades = "";
    }
Run Code Online (Sandbox Code Playgroud)

它抱怨所有'if(strType =="01")'行,我不确定是什么.我曾想过为此使用一个开关,但似乎也遇到了同样的问题.

有任何想法吗?

c# unassigned-variable

3
推荐指数
1
解决办法
1万
查看次数

使用未分配的局部变量`total`

我希望有一个所有间隔的总和,但我写这个代码我有一个错误说明:使用未分配的局部变量total?

enter TimeSpan total;
foreach (var grp in query)
{
  TimeSpan interval = TimeSpan.FromMinutes(grp.Minuut); 
  TimeSpan intervalH = TimeSpan.FromHours(grp.Sum);

  interval = interval + intervalH;
  total += interval;
  string timeInterval = interval.ToString();   
  dataGridView2.Rows.Add(i++, grp.Id, grp.Sum, grp.Minuut,timeInterval);
}
Run Code Online (Sandbox Code Playgroud)

c# local-variables unassigned-variable

2
推荐指数
1
解决办法
6556
查看次数

C# 错误 CS0165:使用未分配的局部变量 - 忽略逻辑和输出引用

四处搜索后,我似乎无法找到为什么 C# 编译器抱怨本地变量dteDest 在行中未分配

if (dteSrc == dteDest) {
Run Code Online (Sandbox Code Playgroud)

如果我更换线路,错误就会消失

DateTime dteSrc, dteDest;
Run Code Online (Sandbox Code Playgroud)

和

DateTime dteSrc, dteDest = DateTime.MinValue;
Run Code Online (Sandbox Code Playgroud)

据我所知,如果 dteDest 没有被 DateTime.TryParse 初始化,它是一个 out 参数,那么代码永远不会到达比较行。

我的逻辑是:

  1. 如果 currentDataObj 为 null,则 booHaveOrigDate 为 false,第一个if失败
  2. 如果 currentDataObj 不为 null 但无法转换为 DateTime,则 booHaveOrigDate 为 false,如果失败则为第一个
  3. 如果 DateTime.TryParse 无法转换为 DateTime,则它将返回 false,这与 && 意味着永远不会使用 dteDest。

简单示例代码

void StrangeLogic(object srcData, object currentDataObj) {
   DateTime dteSrc, dteDest;

   bool booHaveNewDate = DateTime.TryParse(srcData.ToString(), out dteSrc);
   bool booHaveOrigDate = (currentDataObj != null) 
                          && …
Run Code Online (Sandbox Code Playgroud)

c# tryparse unassigned-variable

2
推荐指数
1
解决办法
2万
查看次数

数学函数,未分配的变量?

我正在寻找一种方法,在为方程中的变量赋值之前,将多个数学函数加在一起.
我这样做是因为我需要优化我的代码,并且我希望每次都为变量分配不同的值.我想要做的一个例子:

  1. f(x, y) = x + 2y

  2. g(x, y) = 3x - y

  3. 增加f(x, y) + g(x, y)得到h(x, y),所以f(x, y) + g(x, y) = h(x, y) = 4x + y

  4. 现在我有h(x, y),我需要多个值h(x, y)

x = 4; y = 3, h(x, y) = 19
x = 1, y = 0, h(x, y) = 4
Run Code Online (Sandbox Code Playgroud)

等等

这可能吗?我试图将它们创建为字符串,添加字符串,然后删除引号以评估总和,但这不起作用.我试图以这种方式做我的方法,因为我想优化我的代码.如果我能够在评估它之前创建我的最终函数(h(x, y)在这种情况下就是这样),那将非常有帮助.

编辑:我正在添加(e**(x + y)),所以使用矩阵的线性解决方案不起作用:/

python function unassigned-variable

2
推荐指数
1
解决办法
982
查看次数

使用未分配的变量(字符串)

我有一段迭代XML属性的代码:

string groupName;
do
{
    switch (/* ... */)
    {
        case "NAME":
            groupName = thisNavigator.Value;
            break;
        case "HINT":
            // use groupName
Run Code Online (Sandbox Code Playgroud)

但是这样我得到了使用未分配变量的错误.如果我为groupName分配了一些东西,那么我就无法改变它,因为这就是字符串在C#中的工作方式.任何解决方法?

c# string unassigned-variable

1
推荐指数
1
解决办法
8344
查看次数

C#Visual Basic继承

我刚刚开始使用C#,我已经坚持了两个星期这个问题.我有一个主窗体,从类和子类获取值.我的问题是当我尝试创建一个CorporateClass的对象时,VB告诉我我的两个变量(CarSizeInteger和DiscountInteger)是未分配的.我的问题是为什么.我在程序的早期实现了它们.救命!我绝望地被卡住了!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace EX02_CarRentals
{
    public partial class RentalForm : Form
    {
        public RentalForm()
        {
            InitializeComponent();
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void CalculateButton_Click(object sender, EventArgs e)
        {
            int DaysInteger, BeginningOdometerInteger, EndingOdometerInteger, CarSizeInteger, DiscountInteger;

            if (LicenseTextBox.Text != "")

                if (CompactRadioButton.Checked || MidSizeRadioButton.Checked || LuxuryRadioButton.Checked)
                {
                    int.TryParse(DaysRentedTextBox.Text, out DaysInteger);

                    int.TryParse(BeginningOdometerTextBox.Text, out BeginningOdometerInteger);

                    if (BeginningOdometerInteger > 0)
                    {
                        int.TryParse(EndingOdometerTextBox.Text, out EndingOdometerInteger);
                        if …
Run Code Online (Sandbox Code Playgroud)

c# variables unassigned-variable

0
推荐指数
1
解决办法
137
查看次数

尝试在C#中使用IOrderedEnumerable命令列表

我试图读取.csv文件,进行一些格式化,将每一行拆分为其列数据,并将新的分离列数据数组添加到数组列表中.然后我想以不同的方式订购列表.目前只是按用户名按字母顺序升序.

这是我到目前为止所尝试的:

// create list for storing arrays
List<string[]> users;

string[] lineData;
string line;

// read in stremreader
System.IO.StreamReader file = new System.IO.StreamReader("dcpmc_whitelist.csv");
// loop through each line and remove any speech marks
while((line = file.ReadLine()) != null)
{
    // remove speech marks from each line
    line = line.Replace("\"", "");

    // split line into each column
    lineData = line.Split(';');

    // add each element of split array to the list of arrays
    users.Add(lineData);

}

IOrderedEnumerable<String[]> usersByUsername = users.OrderBy(user => user[1]);

Console.WriteLine(usersByUsername); …
Run Code Online (Sandbox Code Playgroud)

c# sorting console-application visual-studio-2010 unassigned-variable

0
推荐指数
1
解决办法
147
查看次数

如何摆脱编译错误:C#使用未分配的局部变量?

我有一些问题,为什么C#不喜欢这coordinates[j]部分以及我能做些什么.

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Dropbox\ProjectEuler\102\p102_triangles.txt");
string[] coordinates_str;
double[] coordinates; //Contains the coordinates for each line A1(x,y), A2(x,y), A3(x,y)
long ln = lines.Length;
Console.WriteLine("Length: " + ln.ToString());
for (int i = 0; i < ln; i++)
{
    Console.Write(i);
    Console.Write(lines[i]);
    coordinates_str = lines[i].Split(',');
    for (int j = 0; j < 6; j++)
    {
        coordinates[j] = Convert.ToDouble(coordinates_str[j]);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# compiler-errors unassigned-variable

0
推荐指数
1
解决办法
389
查看次数

如何显示可为空的DateTime类型的字符串表示?

用户提供"从"和"到"月份以生成报告(例如,从1个月回到13个月;如果他们在2016年2月15日选择此项,则返回值为2015年1月1日, 2016年1月1日).

我想允许用户从"from"或"to"组合框中选择最远的后面或最近的月份.我只想用最远的时间作为"从"和最接近的时间作为"to"以避免他们的混淆(他们可以做任何看起来很自然的事情).

所以我从这段代码开始:

int fromMonths = Convert.ToInt32(comboBoxProduceUsageFrom.Text);
DateTime RptParamsNominalFromDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(fromMonths, nextGenerateAndSendDate);
int toMonths = Convert.ToInt32(comboBoxProduceUsageTo.Text);
DateTime RptParamsNominalToDate = ReportSchedulerConstsAndUtils.SubtractMonthsFrom(toMonths, nextGenerateAndSendDate);
Run Code Online (Sandbox Code Playgroud)

..然后我想将"从"日期设置为最晚的时间,并将"到"设置为更接近时间.我第一次尝试这个:

DateTime RptParamsFromDate;
DateTime RptParamsToDate;
if (RptParamsNominalFromDate > RptParamsNominalToDate)
{
    RptParamsFromDate = RptParamsNominalToDate;
    RptParamsToDate = RptParamsNominalFromDate;
}
else if (RptParamsNominalToDate > RptParamsNominalFromDate)
{
    RptParamsFromDate = RptParamsNominalFromDate;
    RptParamsToDate = RptParamsNominalToDate;
}
Run Code Online (Sandbox Code Playgroud)

...但是失败了," 使用未分配的局部变量'RptParamsFromDate'"(和"RptParamsToDate "的错误相同).

所以我尝试给DateTimes一个值/非值,如下所示:

DateTime RptParamsFromDate = null;
DateTime RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)

...但是这给了我," 无法将null转换为'System.DateTime',因为它是一个不可为空的值类型 "

所以我再次设置动作并尝试对DateTimes进行nullablizing:

DateTime? RptParamsFromDate = null;
DateTime? RptParamsToDate = null;
Run Code Online (Sandbox Code Playgroud)

...但后来我得到," 'System.Nullable'不包含'ToLongDateString'的定义,并且没有扩展方法'ToLongDateString'接受类型'System.Nullable'的第一个参数'(你是否错过了使用指令或汇编参考?) …

c# datetime nullable unassigned-variable

0
推荐指数
1
解决办法
248
查看次数