עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C++, משתגע מ-error LNK2005 - already defined.

Featured Replies

פורסם

אני מתחרפן כבר מזה.

יש לי CLASS שאני צריך שישתמש בפונקציה מסויימת שהגדרתי ב-ExFuntions.h, ויש לי פונקציה שנקראת MoveUnit בקובץ GameControl.CPP אחר שמשתמשת גם כן באותה פונקציה מ-ExFuntions.h.

הבעיה שהפונקציה MoveUnit משתמשת גם ב-CLASS שלי, וכשאני עושה ב-Include GameControl.CPP ל-ExFuntions.h, אני מקבל את error LNK2005 והוא אומר לי already defined.

ניסיתי לעבוד עם ה-ifndef אבל הוא לא פותר את הבעיה משום מה.

מה עושים?

פורסם

אתה אף-פעם לא עושה include לקובץ cpp.

classes צריכים להיות מוגדרים בקובץ header,

קבצים שמשתמשים בהם עושים include לקובץ ההגדרה של ה- class.

פורסם
  • מחבר

לא עשיתי INCLUDE לקובץ CPP, עשיתי INCLUDE לקובץ HEADER בתוך קובץ CPP.

פורסם
  • מחבר

כתבתי בקובץ ה-CPP של ה-CLASS ובקובץ GameControl.CPP את הקוד הבא:


#ifndef EXFUNCTIONS_H
#define EXFUNCTIONS_H
#include "ExFunctions.h"
#endif

פורסם

זה אמור לעטוף את התוכן של קובץ ה-H, לא את ה-includeים שאתה עושה לו

פורסם
  • מחבר

אוקיי, שיניתי את ה-ifndef כמו שאמרת.

את ה-include-ים איפה אני עושה?

ב-H?

או ב-CPP?

פורסם

ExFunctions.h צריך בכלליות להיראות ככה -

#ifndef EXFUNCTIONS_H
#define EXFUNCTIONS_H

//your code here

#endif

וה-include בתוך GameControl.CPP צריך להיות סטנדרטי לחלוטין -

#include "ExFunctions.h"

פורסם
  • מחבר

כרגע זה ככה:


#ifndef EXFUNCTIONS_H
#define EXFUNCTIONS_H
#include <cstring>
#include <iostream>
#include <windows.h>
#include <string>
#include <cmath>
#include "Structs.h"
using namespace std;

float Distance(location_strct Location1, location_strct Location2)
.......
float Distance(float X, float Y, float newX, float newY)

.........
float Distance(int X, int Y, int newX, int newY)
........
// ***** gets input from user *******
int getInput()
.....
#endif

ויש לי include ב-GameControl.cpp אבל ברגע שאני מוסיף include גם ל-CPP של ה-Class שלי, אני בכל זאת מקבל הודעת שגיאה בשלב ה-linking.


Unit.obj : error LNK2005: "float __cdecl Distance(struct location_strct,struct location_strct)" (?Distance@@YAMUlocation_strct@@0@Z) already defined in GameControl.obj
Unit.obj : error LNK2005: "float __cdecl Distance(float,float,float,float)" (?Distance@@YAMMMMM@Z) already defined in GameControl.obj
Unit.obj : error LNK2005: "float __cdecl Distance(int,int,int,int)" (?Distance@@YAMHHHH@Z) already defined in GameControl.obj
Unit.obj : error LNK2005: "int __cdecl getInput(void)" (?getInput@@YAHXZ) already defined in GameControl.obj

פורסם

למה אתה מממש בתוך ה-H? תוציא משם את המימוש או שתגדיר את הפונקציות כ-inline

פורסם
  • מחבר

ואללה, זה פתר באמת את הבעיה.

תודה. :xyxthumbs:

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.