2013-01-24 69 views
0

我正在爲一個課程做一個程序,老師給了我們一個我們不得不實施的Cpp文件。除了Main之外,所有的都是由他寫的,但是我得到了一個奇怪的錯誤。任何幫助都會很棒。這是我的代碼。C++未解析的令牌問題

// ************************************************************************** 
// 
// Counter.cpp 
// 
// Defines and tests class CounterType, which is used to count things. 
// CounterType contains both a default constructor and a constructor that 
// sets the count to a specified value, plus methods to increment, decrement, 
// return, and output the count. The count is always nonnegative. 
// 
// ************************************************************************** 
#include <iostream> 
#include <fstream> 
#include <cstdlib> 
using namespace std; 

class CounterType 
{ 
public: 
    CounterType(); 
    //Initializes the count to 0. 

    CounterType(int initCount); 
    //Precondition: initCount holds the initial value for the count. 
    //Postcondition: 
    // If initCount > 0,initializes the count to initCount. 
    // If initCount <= 0,initializes the count to 0. 

    void increment(); 
    //Postcondition: 
    // The count is one more than it was. 

    void decrement(); 
    //Postcondition: 
    // If the count was positive, it is now one less than it was. 
    // If the count was 0, it is still 0 

    int getCount(); 
    void output(ostream& outStream); 
    //Precondition: outStream is ready to write to 
    //Postcondition: count has been written to outStream 

private: 
    int count; 

}; 

void increment(); 
void decrement(); 
int getCount(); 
void output(ostream& outStream); 

int main() 
{ 
    CounterType Test; 

    increment(); 
    decrement(); 
    getCount(); 

} 

CounterType::CounterType() 
{ 
    count = 0; 
} 

CounterType::CounterType(int initCount) 
{ 
    if (initCount >= 0) 
     count = initCount; 
    else 
     count = 0; 
} 

void CounterType::increment() 
{ 
    count++; 
} 

void CounterType::decrement() 
{ 
    if (count > 0) 
     count--; 
} 

int CounterType::getCount() 
{ 
    return count; 
} 

void CounterType::output(ostream& outStream) 
{ 
    outStream << count; 
} 

這裏是錯誤。

錯誤1個錯誤LNK2028:無法解析的標記(0A000330) 「無效__cdecl遞減(無效)」 函數引用 「INT __cdecl主要(無效)」(主@@ $(遞減@@ $$ FYAXXZ?)? $ HYAHXZ)j:\ MCM 10.05 \ 10.05 MCM \ MCM10.obj MCM 10.05

+1

哦,男人!你需要一些C++教師或一本書! :) – Ajay

回答

5

您聲明全局函數increment()decrement(),並且getCount()你永遠無法定義。你會因爲在main()中調用它們而導致鏈接錯誤,並且鏈接器找不到它們的定義。

你可能意味着調用Counter對象的成員函數,像這樣:

int main() 
{ 
    CounterType Test; 

    Test.increment(); 
    Test.decrement(); 
    Test.getCount(); 
} 

如果是那樣的話,你應該刪除的全局函數的聲明:

// THESE DECLARATIONS BEFORE main() SHOULD NOT BE THERE! JUST REMOVE THEM 
// void increment(); 
// void decrement(); 
// int getCount(); 
// void output(ostream& outStream); 
1

你聲明一個名爲decrement()的全局函數並且不定義它。

存在CounterType::decrement(),但這是一個不同的功能。

increment()getCount()也是如此。

1

你做2個錯誤:

1)通過查看你的代碼看來,你還聲明如下功能

void increment(); 
void decrement(); 
int getCount(); 
void output(ostream& outStream); 

您已經提供了聲明類,所以現在無須再申報。

2) 你以這種方式被調用函數在main

increment(); 
decrement(); 
getCount(); 

這可能不是你想要做的,因爲調用以這種方式將調用全局函數。調用類函數的正確方法是通過類對象

Test.increment(); 
Test.decrement(); 
Test.getCount(); 

僅僅通過糾正這個2度的變化,你的計劃是準備好去。 :)