2017-07-18 137 views
2

我是C++新手,目前我正在學習考試,在Visual Studio中使用C++進行混亂並嘗試了一下。通常我使用Java。C++新手類的成員函數

我寫了一個簡單的類,看看事情如何以及是否正常工作:

class Point 
{ 
private: 
    int x; 
    int y; 

public: 
    Point(int arg1, int arg2) 
    { 
     x = arg1; 
     y = arg2; 
    } 
}; 

我試圖x和y的2個簡單的成員函數只需雙擊存儲在x和y變量的值。

首先我嘗試這樣做:

void doubleX() 
{ 
    x *= 2; 
}; 

void doubleY() 
{ 
    y *= 2; 
}; 

然後我嘗試這樣的:

void doubleX() 
{ 
    Point::x = 2 * Point::x; 
}; 

void doubleY() 
{ 
    Point::y = 2 * Point2::y; 
}; 

兩者都放在裏面類定義。

雖然通過建設的VisualStudio它alwas給了我這個錯誤警告: 「錯誤C3867‘點:: doubleX’:非標準語法;使用‘&’創建成員指針」

想招惹周圍還有地址指針,但是......我真的沒有線索。 我想我知道指針基本上是如何工作的,但我不知道如何在我的案例中使用它。

對此問題的任何快速解決方案和解釋?

在此先感謝!

編輯:這裏是我的全部代碼,問題出在主現在

#include "stdafx.h" 
#include <iostream> 

using namespace std; 

class Point 
{ 
public: 
    int x; 
    int y; 

    Point(int arg1, int arg2) 
    { 
     x = arg1; 
     y = arg2; 
    } 

    void doubleX() 
    { 
     x *= 2; 
    }; 

    void doubleY() 
    { 
     y *= 2; 
    }; 
}; 

int main() 
{ 
    Point p(1,1); 

    int &x = p.x; 
    int &y = p.y; 

    cout << x << "|" << y; 

    p.doubleX; p.doubleY; //error message here 

    cout << x << "|" << y; 

    cin.get(); 
} 
+2

什麼是'Point2'?爲了引用類成員,你可以使用'this->'或者把它放在外面(就像在Java中一樣) – UnholySheep

+1

你必須聲明你的成員函數_在類定義中 - 從你粘貼的東西中,它一點也不清楚你做到了。 – Useless

+0

您不可能從該代碼中獲取該錯誤。你應該開始[這裏](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – molbdnilo

回答

4

也許你沒有申報成員函數裏面的這個類的定義?這是基於你的類生成完整的例子:

#include <iostream> 


class Point 
{ 
private: 
    int x; 
    int y; 

public: 
    Point(int arg1, int arg2) 
    { 
     x = arg1; 
     y = arg2; 
    } 

    void doubleX() 
    { 
     x *= 2; /* or this->x *= 2; */ 
    } 

    void doubleY() 
    { 
     y *= 2; 
    } 

    int getX() 
    { 
     return x; 
    } 

    int getY() 
    { 
     return y; 
    } 
}; 


int main() 
{ 
    Point p(2, 3); 

    std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl; 

    p.doubleX(); 
    p.doubleY(); 

    std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl; 

    return 0; 
} 

你可以把這個在main.cpp文件,編譯並運行它。我用g++編譯器測試過,它工作正常。

+0

哦,現在我看到實際問題出現在我的主要方法中,抱歉的傢伙應該發佈整個代碼。 – GenerationLost

+0

'int main() { \t Point p(1,1); \t cout << p.x <<「|」 << p.y; \t p.doubleX; p.doubleY; \t cout << p.x <<「|」 << p.y; \t cin.get(); }' 這是我的主要,成員是公開的,所以爲什麼我不能用「p.x」訪問它們? – GenerationLost

+0

這很容易找到:)。問題不在於訪問「p.x」和「p.y」。 'doubleX'和'doubleY'是**函數**!所以,你必須調用它們像'p.doubleX(); p.doubleY();'。 – Valy

-2

在你的第二套操作

void doubleX() 
{ 
    Point2::x = 2 * Point2::x; 
}; 

void doubleY() 
{ 
    Point2::y = 2 * Point2::y; 
}; 

如果你想他們是在Point類成員函數, Point::y ...這不是你應該如何訪問成員數據。只有靜態成員變量可以像那樣訪問。正確的方法是

void doubleX() 
{ 
    this->x = 2 * this->x; 
}; 

void doubleY() 
{ 
    this->y = 2 * this->y; 
}; 

這就是使用this指針。

1

不能因信譽發表評論,但它似乎VC++輸出你所說的錯誤消息,如果您嘗試調用

Point::doubleX 

下面是一個輸出的一個活生生的例子: http://rextester.com/ZLCEW66682

您應該創建類的一個實例並使用parens調用函數

+0

這就是我所做的,對不起,我編輯了我的入門帖子並添加了我的主要方法,我稱之爲'p.doubleX; p.doubleY' – GenerationLost

+0

您應該調用函數p.doubleX()和p.doubleY(),包括parens –

+0

耶穌基督,謝謝很多兄弟!現在我真的感到非常愚蠢。我現在去喝點咖啡.......... – GenerationLost

2

Valy給出的答案是正確的。但我想提醒你C++爲你提供了另一種聲明和定義方法的選擇,即在類聲明中聲明方法並在類聲明外定義它們。這使您可以輕鬆地分離接口和實現到.h.cpp文件,分別如下所示:

Point.h

class Point 
{ 
private: 
    int x; 
    int y; 

public: 
    Point(int arg1, int arg2); 
    void doubleX(); 
    void doubleY(); 
    int getX(); 
    int getY(); 
}; 

Point.cpp

#include "Point.h" 

Point::Point(int arg1, int arg2) 
{ 
    x = arg1; 
    y = arg2; 
} 

void Point::doubleX() 
{ 
    x *= 2; 
} 

void Point::doubleY() 
{ 
    y *= 2; 
} 

int Point::getX() 
{ 
    return x; 
} 

int Point::getY() 
{ 
    return y; 
} 

// PointTest.cpp如何編譯:

g++ -o PointTest PointTest.cpp Point.cpp 
+0

感謝您的建議!進入標題使用和內聯/ makros將成爲下一個主題,我將通過 – GenerationLost

+1

是的,這當然是更好的解決方案,我想自己寫這樣的。我只想給他一個快速解決方案,而不是引入太多新的概念。 – Valy